ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/concurrent/Lock.java
Revision: 1.1
Committed: Wed May 14 21:30:47 2003 UTC (21 years ago) by tim
Branch: MAIN
Log Message:
Moved main source rooted at . to ./src/main
Moved test source rooted at ./etc/testcases to ./src/test

File Contents

# User Rev Content
1 tim 1.1 package java.util.concurrent;
2    
3     /**
4     * <tt>Lock</tt> implementations provide more flexible locking operations than
5     * can be obtained using <tt>synchronized</tt> methods and statements.
6     *
7     * <p>A lock is a tool for controlling access to a shared
8     * resource by multiple threads. Commonly, a lock provides exclusive access
9     * to a shared resource: only one thread at a time can acquire the
10     * lock and all access to the shared resource requires that the lock be
11     * acquired first. However, some locks may allow concurrent access to a shared
12     * resource, such as the read lock of a {@link ReadWriteLock}.
13     *
14     * <p>The use of <tt>synchronized</tt> methods or statements provides
15     * access to the implicit monitor lock associated with every object, but
16     * forces all lock acquisition and release to occur in a block-structured way:
17     * when multiple locks are acquired they must be released in the opposite
18     * order, and all locks must be released in the same lexical scope in which
19     * they were acquired.
20     *
21     * <p>While the scoping mechanism for <tt>synchronized</tt> methods and
22     * statements makes it much easier to program with monitor locks,
23     * and helps avoid many common programming errors involving locks, there are
24     * rare occasions where you need to work with locks in a more flexible way. For
25     * example, some advanced algorithms for traversing concurrently accessed data
26     * structures require the use of what is called &quot;hand-over-hand&quot; or
27     * &quot;chain locking&quot;: you acquire the lock of node A, then node B,
28     * then release A and acquire C, then release B and acquire D and so on.
29     * Implementations of the <tt>Lock</tt> interface facilitate the use of such
30     * advanced algorithms by allowing a lock to be acquired and released in
31     * different scopes, and allowing multiple locks to be acquired and released
32     * in any order.
33     *
34     * <p>With this increased flexibilty comes
35     * additional responsibility as the absence of block-structured locking
36     * removes the automatic release of locks that occurs with
37     * <tt>synchronized</tt> methods and statements. For the simplest usage
38     * the following idiom should be used:
39     * <pre><tt> Lock l = ...;
40     * l.lock();
41     * try {
42     * // access the resource protected by this lock
43     * } catch ( ... ) {
44     * // ensure consistency before releasing lock
45     * } finally {
46     * l.unlock();
47     * }
48     * </tt></pre>
49     * This mimics the automatic release property of built-in monitor locks, and
50     * carries with it the same responsibility for ensuring that if an exception
51     * occurs then the resource is left in a consistent state before the lock
52     * is released.
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     * This additionally functionality is also extended to built-in monitor
61     * 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     *
112     *
113     * @see ReentrantLock
114     * @see Condition
115     * @see ReadWriteLock
116     * @see Locks
117     *
118     * @since 1.5
119     * @spec JSR-166
120     * @revised $Date: 2003/01/30 22:12:40 $
121     * @editor $Author: dholmes $
122     *
123     */
124     public interface Lock {
125    
126     /**
127     * Acquires the lock.
128     * <p>Acquires the lock if it is available and returns immediately.
129     * <p>If the lock is not available then
130     * the current thread becomes disabled for thread scheduling
131     * purposes and lies dormant until the lock has been acquired.
132     * <p><b>Implementation Considerations</b>
133     * <p>A <tt>Lock</tt> implementation may be able to detect
134     * erroneous use of the lock, such as an invocation that would cause
135     * deadlock, and may throw an (unchecked) exception in such circumstances.
136     * The circumstances and the exception type must be documented by that
137     * <tt>Lock</tt> implementation.
138     *
139     */
140     void lock();
141    
142     /**
143     * Acquires the lock unless the current thread is
144     * {@link Thread#interrupt interrupted}.
145     * <p>Acquires the lock if it is available and returns immediately.
146     * <p>If the lock is not available then
147     * the current thread becomes disabled for thread scheduling
148     * purposes and lies dormant until one of two things happens:
149     * <ul>
150     * <li> The lock is acquired by the current thread; or
151     * <li> Some other thread {@link Thread#interrupt interrupts} the current
152     * thread, and interruption of lock acquisition is supported.
153     * </ul>
154     * <p>If the current thread:
155     * <ul>
156     * <li>has its interrupted status set on entry to this method; or
157     * <li>is {@link Thread#interrupt interrupted} while waiting to acquire
158     * the lock, and interruption of lock acquisition is supported,
159     * </ul>
160     * then {@link InterruptedException} is thrown and the current thread's
161     * interrupted status is cleared.
162     *
163     * <p><b>Implementation Considerations</b>
164     * <p>The ability to interrupt a lock acquisition in some implementations
165     * may not be possible, and if possible could reasonably be foreseen to
166     * be an expensive operation.
167     * The programmer should be aware that this may be the case. An
168     * implementation should document when this is the case.
169     *
170     * <p>A <tt>Lock</tt> implementation may be able to detect
171     * erroneous use of the lock, such as an invocation that would cause
172     * deadlock, and may throw an (unchecked) exception in such circumstances.
173     * The circumstances and the exception type must be documented by that
174     * <tt>Lock</tt> implementation.
175     *
176     * @throws InterruptedException if the current thread is interrupted
177     * (and interruption of lock acquisition is supported).
178     *
179     * @see Thread#interrupt
180     *
181     */
182     void lockInterruptibly() throws InterruptedException;
183    
184    
185     /**
186     * Acquires the lock only if it is free at the time of invocation.
187     * <p>Acquires the lock if it is available and returns immediately
188     * with the value <tt>true</tt>.
189     * <p>If the lock is not available then this method will return
190     * immediately with the value <tt>false</tt>.
191     * <p>A typical usage idiom for this method would be:
192     * <pre>
193     * Lock lock = ...;
194     * if (lock.tryLock()) {
195     * try {
196     * // manipulate protected state
197     * } finally {
198     * lock.unlock();
199     * }
200     * } else {
201     * // perform alternative actions
202     * }
203     * </pre>
204     * This usage ensures that the lock is unlocked if it was acquired, and
205     * doesn't try to unlock if the lock was not acquired.
206     *
207     * @return <tt>true</tt> if the lock was acquired and <tt>false</tt>
208     * otherwise.
209     */
210     boolean tryLock();
211    
212     /**
213     * Acquires the lock if it is free within the given waiting time and the
214     * current thread has not been {@link Thread#interrupt interrupted}.
215     * <p>Acquires the lock if it is available and returns immediately
216     * with the value <tt>true</tt>.
217     * <p>If the lock is not available then
218     * the current thread becomes disabled for thread scheduling
219     * purposes and lies dormant until one of three things happens:
220     * <ul>
221     * <li> The lock is acquired by the current thread; or
222     * <li> Some other thread {@link Thread#interrupt interrupts} the current
223     * thread, and interruption of lock acquisition is supported; or
224     * <li> The specified waiting time elapses
225     * </ul>
226     * <p>If the lock is acquired then the value <tt>true</tt> is returned.
227     * <p>If the current thread:
228     * <ul>
229     * <li>has its interrupted status set on entry to this method; or
230     * <li>is {@link Thread#interrupt interrupted} while waiting to acquire
231     * the lock, and interruption of lock acquisition is supported,
232     * </ul>
233     * then {@link InterruptedException} is thrown and the current thread's
234     * interrupted status is cleared.
235     * <p>If the specified waiting time elapses then the value <tt>false</tt>
236     * is returned.
237     * The given waiting time is a best-effort lower bound. If the time is
238     * less than or equal to zero, the method will not wait at all.
239     *
240     * <p><b>Implementation Considerations</b>
241     * <p>The ability to interrupt a lock acquisition in some implementations
242     * may not be possible, and if possible could reasonably be foreseen to
243     * be an expensive operation.
244     * The programmer should be aware that this may be the case. An
245     * implementation should document when this is the case.
246     *
247     * <p>A <tt>Lock</tt> implementation may be able to detect
248     * erroneous use of the lock, such as an invocation that would cause
249     * deadlock, and may throw an (unchecked) exception in such circumstances.
250     * The circumstances and the exception type must be documented by that
251     * <tt>Lock</tt> implementation.
252     *
253     * @param timeout the maximum time to wait for the lock
254     * @param granularity the time unit of the <tt>timeout</tt> argument.
255     * @return <tt>true</tt> if the lock was acquired and <tt>false</tt>
256     * if the waiting time elapsed before the lock was acquired.
257     *
258     * @throws InterruptedException if the current thread is interrupted
259     * while trying to acquire the lock (and interruption of lock
260     * acquisition is supported).
261     *
262     * @see Thread#interrupt
263     *
264     */
265     boolean tryLock(long timeout, TimeUnit granularity)
266     throws InterruptedException;
267    
268     /**
269     * Releases the lock.
270     * <p><b>Implementation Considerations</b>
271     * <p>A <tt>Lock</tt> implementation will usually impose
272     * restrictions on which thread can release a lock (typically only the
273     * holder of the lock can release it) and may throw
274     * an (unchecked) exception if the restriction is violated.
275     * Any restrictions and the exception
276     * type must be documented by that <tt>Lock</tt> implementation.
277     */
278     void unlock();
279    
280     /**
281     * Returns a {@link Condition} instance that is bound to this <tt>Lock</tt>
282     * instance.
283     * <p>Conditions are primarily used with the built-in locking provided by
284     * <tt>synchronized</tt> methods and statements
285     * (see {@link Locks#newConditionFor}), but in some rare circumstances it
286     * can be useful to wait for a condition when working with a data
287     * structure that is accessed using a stand-alone <tt>Lock</tt> instance
288     * (see {@link ReentrantLock}).
289     * <p>Before waiting on the condition the lock must be held by the
290     * current thread.
291     * A call to {@link Condition#await()} will atomically release the lock
292     * before waiting and re-acquire the lock before the wait returns.
293     * <p><b>Implementation Considerations</b>
294     * <p>The exact operation of the {@link Condition} instance depends on the
295     * <tt>Lock</tt> implementation and must be documented by that
296     * implementation.
297     *
298     * @return A {@link Condition} instance for this <tt>Lock</tt> instance.
299     * @throws UnsupportedOperationException if this <tt>Lock</tt>
300     * implementation does not support conditions.
301     */
302     Condition newCondition();
303    
304     }
305    
306    
307    
308    
309    
310    
311    
312    
313    
314    
315    
316