ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/concurrent/Lock.java
Revision: 1.4
Committed: Wed Jun 11 23:03:06 2003 UTC (21 years ago) by dholmes
Branch: MAIN
Changes since 1.3: +17 -9 lines
Log Message:
Updated interrupt semantics

File Contents

# User Rev Content
1 dl 1.2 /*
2     * Written by Doug Lea with assistance from members of JCP JSR-166
3     * Expert Group and released to the public domain. Use, modify, and
4     * redistribute this code in any way without acknowledgement.
5     */
6    
7 tim 1.1 package java.util.concurrent;
8    
9     /**
10     * <tt>Lock</tt> implementations provide more flexible locking operations than
11     * can be obtained using <tt>synchronized</tt> methods and statements.
12     *
13     * <p>A lock is a tool for controlling access to a shared
14     * resource by multiple threads. Commonly, a lock provides exclusive access
15     * to a shared resource: only one thread at a time can acquire the
16     * lock and all access to the shared resource requires that the lock be
17     * acquired first. However, some locks may allow concurrent access to a shared
18     * resource, such as the read lock of a {@link ReadWriteLock}.
19     *
20     * <p>The use of <tt>synchronized</tt> methods or statements provides
21     * access to the implicit monitor lock associated with every object, but
22     * forces all lock acquisition and release to occur in a block-structured way:
23     * when multiple locks are acquired they must be released in the opposite
24     * order, and all locks must be released in the same lexical scope in which
25     * they were acquired.
26     *
27     * <p>While the scoping mechanism for <tt>synchronized</tt> methods and
28     * statements makes it much easier to program with monitor locks,
29     * and helps avoid many common programming errors involving locks, there are
30     * rare occasions where you need to work with locks in a more flexible way. For
31     * example, some advanced algorithms for traversing concurrently accessed data
32     * structures require the use of what is called &quot;hand-over-hand&quot; or
33     * &quot;chain locking&quot;: you acquire the lock of node A, then node B,
34     * then release A and acquire C, then release B and acquire D and so on.
35     * Implementations of the <tt>Lock</tt> interface facilitate the use of such
36     * advanced algorithms by allowing a lock to be acquired and released in
37     * different scopes, and allowing multiple locks to be acquired and released
38     * in any order.
39     *
40     * <p>With this increased flexibilty comes
41     * additional responsibility as the absence of block-structured locking
42     * removes the automatic release of locks that occurs with
43     * <tt>synchronized</tt> methods and statements. For the simplest usage
44     * the following idiom should be used:
45     * <pre><tt> Lock l = ...;
46     * l.lock();
47     * try {
48     * // access the resource protected by this lock
49     * } finally {
50     * l.unlock();
51     * }
52     * </tt></pre>
53     *
54     * <p><tt>Lock</tt> implementations provide additional functionality over the
55     * use
56     * of <tt>synchronized</tt> methods and statements by providing a non-blocking
57     * attempt to acquire a lock ({@link #tryLock()}), an attempt to acquire the
58     * lock that can be interrupted ({@link #lockInterruptibly}, and an attempt
59     * to acquire the lock that can timeout ({@link #tryLock(long, TimeUnit)}).
60 dholmes 1.3 * This additional functionality is also extended to built-in monitor
61 tim 1.1 * locks through the methods of the {@link Locks} utility class.
62     *
63     * <p>A <tt>Lock</tt> class can also provide behavior and semantics that is
64     * quite different from that of the implicit monitor lock, such as guaranteed
65     * ordering,
66     * non-reentrant usage, or deadlock detection. If an implementation provides
67     * such specialised semantics then the implementation must document those
68     * semantics.
69     *
70     * <p>Note that <tt>Lock</tt> instances are just normal objects and can
71     * themselves be used as the target in a <tt>synchronized</tt> statement.
72     * Acquiring the
73     * monitor lock of a <tt>Lock</tt> instance has no specified relationship
74     * with invoking any of the {@link #lock} methods of that instance.
75     * It is recommended that to avoid confusion you never use <tt>Lock</tt>
76     * instances in this way, except within their own implementation.
77     *
78     * <p>Except where noted, passing a <tt>null</tt> value for any parameter
79     * will result in a {@link NullPointerException} being thrown.
80     *
81     * <h3>Memory Synchronization</h3>
82     * <p>All <tt>Lock</tt> implementations <em>must</em> enforce the same
83     * memory synchronization semantics as provided by the built-in monitor lock:
84     * <ul>
85     * <li>A successful lock operation acts like a successful
86     * <tt>monitorEnter</tt> action
87     * <li>A successful <tt>unlock</tt> operation acts like a successful
88     * <tt>monitorExit</tt> action
89     * </ul>
90     * Note that unsuccessful locking and unlocking operations, and reentrant
91     * locking/unlocking operations, do not require any memory synchronization
92     * effects.
93     *
94     * <h3>Implementation Considerations</h3>
95     * <p>It is recognised that the three forms of lock acquisition (interruptible,
96     * non-interruptible, and timed) may differ in their ease of implementation
97     * on some platforms and in their performance characteristics.
98     * In particular, it may be difficult to provide these features and maintain
99     * specific semantics such as ordering guarantees.
100     * Further, the ability to interrupt the acquisition of a lock may not always
101     * be feasible to implement on all platforms.
102     * <p>Consequently, an implementation is not required to define exactly the
103     * same
104     * guarantees or semantics for all three forms of lock acquistion, nor is it
105     * required to support interruption of the actual lock acquisition.
106     * An implementation is required to clearly
107     * document the semantics and guarantees provided by each of the locking
108     * methods. It must also obey the interruption semantics as defined in this
109     * interface, to the extent that interruption of lock acquisition is
110     * supported: which is either totally, or only on method entry.
111 dholmes 1.4 * <p>As interruption generally implies cancellation, and checks for
112     * interruption are often infrequent, an implementation can favor responding
113     * to an interrupt over normal method return. This is true even if it can be
114     * shown that the interrupt occurred after another action may have unblocked
115     * the thread. An implementation should document this behaviour.
116 tim 1.1 *
117     *
118     * @see ReentrantLock
119     * @see Condition
120     * @see ReadWriteLock
121     * @see Locks
122     *
123     * @since 1.5
124     * @spec JSR-166
125 dholmes 1.4 * @revised $Date: 2003/06/09 23:51:33 $
126     * @editor $Author: dholmes $
127 tim 1.1 *
128 dl 1.2 **/
129 tim 1.1 public interface Lock {
130    
131     /**
132     * Acquires the lock.
133     * <p>Acquires the lock if it is available and returns immediately.
134     * <p>If the lock is not available then
135     * the current thread becomes disabled for thread scheduling
136     * purposes and lies dormant until the lock has been acquired.
137     * <p><b>Implementation Considerations</b>
138     * <p>A <tt>Lock</tt> implementation may be able to detect
139     * erroneous use of the lock, such as an invocation that would cause
140     * deadlock, and may throw an (unchecked) exception in such circumstances.
141     * The circumstances and the exception type must be documented by that
142     * <tt>Lock</tt> implementation.
143     *
144 dl 1.2 **/
145     public void lock();
146 tim 1.1
147     /**
148     * Acquires the lock unless the current thread is
149     * {@link Thread#interrupt interrupted}.
150     * <p>Acquires the lock if it is available and returns immediately.
151     * <p>If the lock is not available then
152 dl 1.2 * the current thread thread becomes disabled for thread scheduling
153 tim 1.1 * purposes and lies dormant until one of two things happens:
154     * <ul>
155     * <li> The lock is acquired by the current thread; or
156     * <li> Some other thread {@link Thread#interrupt interrupts} the current
157     * thread, and interruption of lock acquisition is supported.
158     * </ul>
159     * <p>If the current thread:
160     * <ul>
161     * <li>has its interrupted status set on entry to this method; or
162 dholmes 1.4 * <li>is {@link Thread#interrupt interrupted} while acquiring
163 tim 1.1 * the lock, and interruption of lock acquisition is supported,
164     * </ul>
165     * then {@link InterruptedException} is thrown and the current thread's
166     * interrupted status is cleared.
167     *
168     * <p><b>Implementation Considerations</b>
169     * <p>The ability to interrupt a lock acquisition in some implementations
170     * may not be possible, and if possible could reasonably be foreseen to
171     * be an expensive operation.
172     * The programmer should be aware that this may be the case. An
173     * implementation should document when this is the case.
174 dholmes 1.4 * <p>An implementation can favor responding to an interrupt over
175     * normal method return.
176 tim 1.1 * <p>A <tt>Lock</tt> implementation may be able to detect
177     * erroneous use of the lock, such as an invocation that would cause
178     * deadlock, and may throw an (unchecked) exception in such circumstances.
179     * The circumstances and the exception type must be documented by that
180     * <tt>Lock</tt> implementation.
181     *
182     * @throws InterruptedException if the current thread is interrupted
183 dholmes 1.4 * while acquiring the lock (and interruption of lock acquisition is
184     * supported).
185 tim 1.1 *
186     * @see Thread#interrupt
187     *
188 dl 1.2 **/
189     public void lockInterruptibly() throws InterruptedException;
190 tim 1.1
191    
192     /**
193     * Acquires the lock only if it is free at the time of invocation.
194     * <p>Acquires the lock if it is available and returns immediately
195     * with the value <tt>true</tt>.
196     * <p>If the lock is not available then this method will return
197     * immediately with the value <tt>false</tt>.
198     * <p>A typical usage idiom for this method would be:
199     * <pre>
200     * Lock lock = ...;
201     * if (lock.tryLock()) {
202     * try {
203     * // manipulate protected state
204     * } finally {
205     * lock.unlock();
206     * }
207     * } else {
208     * // perform alternative actions
209     * }
210     * </pre>
211     * This usage ensures that the lock is unlocked if it was acquired, and
212     * doesn't try to unlock if the lock was not acquired.
213     *
214     * @return <tt>true</tt> if the lock was acquired and <tt>false</tt>
215     * otherwise.
216 dl 1.2 **/
217     public boolean tryLock();
218 tim 1.1
219     /**
220     * Acquires the lock if it is free within the given waiting time and the
221     * current thread has not been {@link Thread#interrupt interrupted}.
222     * <p>Acquires the lock if it is available and returns immediately
223     * with the value <tt>true</tt>.
224     * <p>If the lock is not available then
225     * the current thread becomes disabled for thread scheduling
226     * purposes and lies dormant until one of three things happens:
227     * <ul>
228     * <li> The lock is acquired by the current thread; or
229     * <li> Some other thread {@link Thread#interrupt interrupts} the current
230     * thread, and interruption of lock acquisition is supported; or
231     * <li> The specified waiting time elapses
232     * </ul>
233     * <p>If the lock is acquired then the value <tt>true</tt> is returned.
234     * <p>If the current thread:
235     * <ul>
236     * <li>has its interrupted status set on entry to this method; or
237 dholmes 1.4 * <li>is {@link Thread#interrupt interrupted} while acquiring
238 tim 1.1 * the lock, and interruption of lock acquisition is supported,
239     * </ul>
240     * then {@link InterruptedException} is thrown and the current thread's
241     * interrupted status is cleared.
242     * <p>If the specified waiting time elapses then the value <tt>false</tt>
243     * is returned.
244     * The given waiting time is a best-effort lower bound. If the time is
245     * less than or equal to zero, the method will not wait at all.
246     *
247     * <p><b>Implementation Considerations</b>
248     * <p>The ability to interrupt a lock acquisition in some implementations
249     * may not be possible, and if possible could reasonably be foreseen to
250     * be an expensive operation.
251     * The programmer should be aware that this may be the case. An
252     * implementation should document when this is the case.
253 dholmes 1.4 * <p>An implementation can favor responding to an interrupt over normal
254     * method return, or reporting a timeout.
255 tim 1.1 * <p>A <tt>Lock</tt> implementation may be able to detect
256     * erroneous use of the lock, such as an invocation that would cause
257     * deadlock, and may throw an (unchecked) exception in such circumstances.
258     * The circumstances and the exception type must be documented by that
259     * <tt>Lock</tt> implementation.
260     *
261 dl 1.2 * @param time the maximum time to wait for the lock
262     * @param unit the time unit of the <tt>time</tt> argument.
263 tim 1.1 * @return <tt>true</tt> if the lock was acquired and <tt>false</tt>
264     * if the waiting time elapsed before the lock was acquired.
265     *
266     * @throws InterruptedException if the current thread is interrupted
267 dholmes 1.4 * while acquiring the lock (and interruption of lock acquisition is
268     * supported).
269 tim 1.1 *
270     * @see Thread#interrupt
271     *
272 dl 1.2 **/
273     public boolean tryLock(long time, TimeUnit unit) throws InterruptedException;
274 tim 1.1
275     /**
276     * Releases the lock.
277     * <p><b>Implementation Considerations</b>
278     * <p>A <tt>Lock</tt> implementation will usually impose
279     * restrictions on which thread can release a lock (typically only the
280     * holder of the lock can release it) and may throw
281     * an (unchecked) exception if the restriction is violated.
282     * Any restrictions and the exception
283     * type must be documented by that <tt>Lock</tt> implementation.
284 dl 1.2 **/
285     public void unlock();
286 tim 1.1
287     /**
288     * Returns a {@link Condition} instance that is bound to this <tt>Lock</tt>
289     * instance.
290     * <p>Conditions are primarily used with the built-in locking provided by
291     * <tt>synchronized</tt> methods and statements
292     * (see {@link Locks#newConditionFor}), but in some rare circumstances it
293     * can be useful to wait for a condition when working with a data
294     * structure that is accessed using a stand-alone <tt>Lock</tt> instance
295     * (see {@link ReentrantLock}).
296     * <p>Before waiting on the condition the lock must be held by the
297     * current thread.
298     * A call to {@link Condition#await()} will atomically release the lock
299     * before waiting and re-acquire the lock before the wait returns.
300     * <p><b>Implementation Considerations</b>
301     * <p>The exact operation of the {@link Condition} instance depends on the
302     * <tt>Lock</tt> implementation and must be documented by that
303     * implementation.
304     *
305     * @return A {@link Condition} instance for this <tt>Lock</tt> instance.
306     * @throws UnsupportedOperationException if this <tt>Lock</tt>
307     * implementation does not support conditions.
308 dl 1.2 **/
309     public Condition newCondition();
310 tim 1.1
311     }
312    
313    
314    
315    
316    
317    
318    
319    
320    
321    
322    
323