jaroslav@1890
|
1 |
/*
|
jaroslav@1890
|
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
jaroslav@1890
|
3 |
*
|
jaroslav@1890
|
4 |
* This code is free software; you can redistribute it and/or modify it
|
jaroslav@1890
|
5 |
* under the terms of the GNU General Public License version 2 only, as
|
jaroslav@1890
|
6 |
* published by the Free Software Foundation. Oracle designates this
|
jaroslav@1890
|
7 |
* particular file as subject to the "Classpath" exception as provided
|
jaroslav@1890
|
8 |
* by Oracle in the LICENSE file that accompanied this code.
|
jaroslav@1890
|
9 |
*
|
jaroslav@1890
|
10 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
jaroslav@1890
|
11 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
jaroslav@1890
|
12 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
jaroslav@1890
|
13 |
* version 2 for more details (a copy is included in the LICENSE file that
|
jaroslav@1890
|
14 |
* accompanied this code).
|
jaroslav@1890
|
15 |
*
|
jaroslav@1890
|
16 |
* You should have received a copy of the GNU General Public License version
|
jaroslav@1890
|
17 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
jaroslav@1890
|
18 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
jaroslav@1890
|
19 |
*
|
jaroslav@1890
|
20 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
jaroslav@1890
|
21 |
* or visit www.oracle.com if you need additional information or have any
|
jaroslav@1890
|
22 |
* questions.
|
jaroslav@1890
|
23 |
*/
|
jaroslav@1890
|
24 |
|
jaroslav@1890
|
25 |
/*
|
jaroslav@1890
|
26 |
* This file is available under and governed by the GNU General Public
|
jaroslav@1890
|
27 |
* License version 2 only, as published by the Free Software Foundation.
|
jaroslav@1890
|
28 |
* However, the following notice accompanied the original version of this
|
jaroslav@1890
|
29 |
* file:
|
jaroslav@1890
|
30 |
*
|
jaroslav@1890
|
31 |
* Written by Doug Lea with assistance from members of JCP JSR-166
|
jaroslav@1890
|
32 |
* Expert Group and released to the public domain, as explained at
|
jaroslav@1890
|
33 |
* http://creativecommons.org/publicdomain/zero/1.0/
|
jaroslav@1890
|
34 |
*/
|
jaroslav@1890
|
35 |
|
jaroslav@1890
|
36 |
package java.util.concurrent.locks;
|
jaroslav@1890
|
37 |
import java.util.concurrent.*;
|
jaroslav@1890
|
38 |
import java.util.Date;
|
jaroslav@1890
|
39 |
|
jaroslav@1890
|
40 |
/**
|
jaroslav@1890
|
41 |
* {@code Condition} factors out the {@code Object} monitor
|
jaroslav@1890
|
42 |
* methods ({@link Object#wait() wait}, {@link Object#notify notify}
|
jaroslav@1890
|
43 |
* and {@link Object#notifyAll notifyAll}) into distinct objects to
|
jaroslav@1890
|
44 |
* give the effect of having multiple wait-sets per object, by
|
jaroslav@1890
|
45 |
* combining them with the use of arbitrary {@link Lock} implementations.
|
jaroslav@1890
|
46 |
* Where a {@code Lock} replaces the use of {@code synchronized} methods
|
jaroslav@1890
|
47 |
* and statements, a {@code Condition} replaces the use of the Object
|
jaroslav@1890
|
48 |
* monitor methods.
|
jaroslav@1890
|
49 |
*
|
jaroslav@1890
|
50 |
* <p>Conditions (also known as <em>condition queues</em> or
|
jaroslav@1890
|
51 |
* <em>condition variables</em>) provide a means for one thread to
|
jaroslav@1890
|
52 |
* suspend execution (to "wait") until notified by another
|
jaroslav@1890
|
53 |
* thread that some state condition may now be true. Because access
|
jaroslav@1890
|
54 |
* to this shared state information occurs in different threads, it
|
jaroslav@1890
|
55 |
* must be protected, so a lock of some form is associated with the
|
jaroslav@1890
|
56 |
* condition. The key property that waiting for a condition provides
|
jaroslav@1890
|
57 |
* is that it <em>atomically</em> releases the associated lock and
|
jaroslav@1890
|
58 |
* suspends the current thread, just like {@code Object.wait}.
|
jaroslav@1890
|
59 |
*
|
jaroslav@1890
|
60 |
* <p>A {@code Condition} instance is intrinsically bound to a lock.
|
jaroslav@1890
|
61 |
* To obtain a {@code Condition} instance for a particular {@link Lock}
|
jaroslav@1890
|
62 |
* instance use its {@link Lock#newCondition newCondition()} method.
|
jaroslav@1890
|
63 |
*
|
jaroslav@1890
|
64 |
* <p>As an example, suppose we have a bounded buffer which supports
|
jaroslav@1890
|
65 |
* {@code put} and {@code take} methods. If a
|
jaroslav@1890
|
66 |
* {@code take} is attempted on an empty buffer, then the thread will block
|
jaroslav@1890
|
67 |
* until an item becomes available; if a {@code put} is attempted on a
|
jaroslav@1890
|
68 |
* full buffer, then the thread will block until a space becomes available.
|
jaroslav@1890
|
69 |
* We would like to keep waiting {@code put} threads and {@code take}
|
jaroslav@1890
|
70 |
* threads in separate wait-sets so that we can use the optimization of
|
jaroslav@1890
|
71 |
* only notifying a single thread at a time when items or spaces become
|
jaroslav@1890
|
72 |
* available in the buffer. This can be achieved using two
|
jaroslav@1890
|
73 |
* {@link Condition} instances.
|
jaroslav@1890
|
74 |
* <pre>
|
jaroslav@1890
|
75 |
* class BoundedBuffer {
|
jaroslav@1890
|
76 |
* <b>final Lock lock = new ReentrantLock();</b>
|
jaroslav@1890
|
77 |
* final Condition notFull = <b>lock.newCondition(); </b>
|
jaroslav@1890
|
78 |
* final Condition notEmpty = <b>lock.newCondition(); </b>
|
jaroslav@1890
|
79 |
*
|
jaroslav@1890
|
80 |
* final Object[] items = new Object[100];
|
jaroslav@1890
|
81 |
* int putptr, takeptr, count;
|
jaroslav@1890
|
82 |
*
|
jaroslav@1890
|
83 |
* public void put(Object x) throws InterruptedException {
|
jaroslav@1890
|
84 |
* <b>lock.lock();
|
jaroslav@1890
|
85 |
* try {</b>
|
jaroslav@1890
|
86 |
* while (count == items.length)
|
jaroslav@1890
|
87 |
* <b>notFull.await();</b>
|
jaroslav@1890
|
88 |
* items[putptr] = x;
|
jaroslav@1890
|
89 |
* if (++putptr == items.length) putptr = 0;
|
jaroslav@1890
|
90 |
* ++count;
|
jaroslav@1890
|
91 |
* <b>notEmpty.signal();</b>
|
jaroslav@1890
|
92 |
* <b>} finally {
|
jaroslav@1890
|
93 |
* lock.unlock();
|
jaroslav@1890
|
94 |
* }</b>
|
jaroslav@1890
|
95 |
* }
|
jaroslav@1890
|
96 |
*
|
jaroslav@1890
|
97 |
* public Object take() throws InterruptedException {
|
jaroslav@1890
|
98 |
* <b>lock.lock();
|
jaroslav@1890
|
99 |
* try {</b>
|
jaroslav@1890
|
100 |
* while (count == 0)
|
jaroslav@1890
|
101 |
* <b>notEmpty.await();</b>
|
jaroslav@1890
|
102 |
* Object x = items[takeptr];
|
jaroslav@1890
|
103 |
* if (++takeptr == items.length) takeptr = 0;
|
jaroslav@1890
|
104 |
* --count;
|
jaroslav@1890
|
105 |
* <b>notFull.signal();</b>
|
jaroslav@1890
|
106 |
* return x;
|
jaroslav@1890
|
107 |
* <b>} finally {
|
jaroslav@1890
|
108 |
* lock.unlock();
|
jaroslav@1890
|
109 |
* }</b>
|
jaroslav@1890
|
110 |
* }
|
jaroslav@1890
|
111 |
* }
|
jaroslav@1890
|
112 |
* </pre>
|
jaroslav@1890
|
113 |
*
|
jaroslav@1890
|
114 |
* (The {@link java.util.concurrent.ArrayBlockingQueue} class provides
|
jaroslav@1890
|
115 |
* this functionality, so there is no reason to implement this
|
jaroslav@1890
|
116 |
* sample usage class.)
|
jaroslav@1890
|
117 |
*
|
jaroslav@1890
|
118 |
* <p>A {@code Condition} implementation can provide behavior and semantics
|
jaroslav@1890
|
119 |
* that is
|
jaroslav@1890
|
120 |
* different from that of the {@code Object} monitor methods, such as
|
jaroslav@1890
|
121 |
* guaranteed ordering for notifications, or not requiring a lock to be held
|
jaroslav@1890
|
122 |
* when performing notifications.
|
jaroslav@1890
|
123 |
* If an implementation provides such specialized semantics then the
|
jaroslav@1890
|
124 |
* implementation must document those semantics.
|
jaroslav@1890
|
125 |
*
|
jaroslav@1890
|
126 |
* <p>Note that {@code Condition} instances are just normal objects and can
|
jaroslav@1890
|
127 |
* themselves be used as the target in a {@code synchronized} statement,
|
jaroslav@1890
|
128 |
* and can have their own monitor {@link Object#wait wait} and
|
jaroslav@1890
|
129 |
* {@link Object#notify notification} methods invoked.
|
jaroslav@1890
|
130 |
* Acquiring the monitor lock of a {@code Condition} instance, or using its
|
jaroslav@1890
|
131 |
* monitor methods, has no specified relationship with acquiring the
|
jaroslav@1890
|
132 |
* {@link Lock} associated with that {@code Condition} or the use of its
|
jaroslav@1890
|
133 |
* {@linkplain #await waiting} and {@linkplain #signal signalling} methods.
|
jaroslav@1890
|
134 |
* It is recommended that to avoid confusion you never use {@code Condition}
|
jaroslav@1890
|
135 |
* instances in this way, except perhaps within their own implementation.
|
jaroslav@1890
|
136 |
*
|
jaroslav@1890
|
137 |
* <p>Except where noted, passing a {@code null} value for any parameter
|
jaroslav@1890
|
138 |
* will result in a {@link NullPointerException} being thrown.
|
jaroslav@1890
|
139 |
*
|
jaroslav@1890
|
140 |
* <h3>Implementation Considerations</h3>
|
jaroslav@1890
|
141 |
*
|
jaroslav@1890
|
142 |
* <p>When waiting upon a {@code Condition}, a "<em>spurious
|
jaroslav@1890
|
143 |
* wakeup</em>" is permitted to occur, in
|
jaroslav@1890
|
144 |
* general, as a concession to the underlying platform semantics.
|
jaroslav@1890
|
145 |
* This has little practical impact on most application programs as a
|
jaroslav@1890
|
146 |
* {@code Condition} should always be waited upon in a loop, testing
|
jaroslav@1890
|
147 |
* the state predicate that is being waited for. An implementation is
|
jaroslav@1890
|
148 |
* free to remove the possibility of spurious wakeups but it is
|
jaroslav@1890
|
149 |
* recommended that applications programmers always assume that they can
|
jaroslav@1890
|
150 |
* occur and so always wait in a loop.
|
jaroslav@1890
|
151 |
*
|
jaroslav@1890
|
152 |
* <p>The three forms of condition waiting
|
jaroslav@1890
|
153 |
* (interruptible, non-interruptible, and timed) may differ in their ease of
|
jaroslav@1890
|
154 |
* implementation on some platforms and in their performance characteristics.
|
jaroslav@1890
|
155 |
* In particular, it may be difficult to provide these features and maintain
|
jaroslav@1890
|
156 |
* specific semantics such as ordering guarantees.
|
jaroslav@1890
|
157 |
* Further, the ability to interrupt the actual suspension of the thread may
|
jaroslav@1890
|
158 |
* not always be feasible to implement on all platforms.
|
jaroslav@1890
|
159 |
*
|
jaroslav@1890
|
160 |
* <p>Consequently, an implementation is not required to define exactly the
|
jaroslav@1890
|
161 |
* same guarantees or semantics for all three forms of waiting, nor is it
|
jaroslav@1890
|
162 |
* required to support interruption of the actual suspension of the thread.
|
jaroslav@1890
|
163 |
*
|
jaroslav@1890
|
164 |
* <p>An implementation is required to
|
jaroslav@1890
|
165 |
* clearly document the semantics and guarantees provided by each of the
|
jaroslav@1890
|
166 |
* waiting methods, and when an implementation does support interruption of
|
jaroslav@1890
|
167 |
* thread suspension then it must obey the interruption semantics as defined
|
jaroslav@1890
|
168 |
* in this interface.
|
jaroslav@1890
|
169 |
*
|
jaroslav@1890
|
170 |
* <p>As interruption generally implies cancellation, and checks for
|
jaroslav@1890
|
171 |
* interruption are often infrequent, an implementation can favor responding
|
jaroslav@1890
|
172 |
* to an interrupt over normal method return. This is true even if it can be
|
jaroslav@1890
|
173 |
* shown that the interrupt occurred after another action that may have
|
jaroslav@1890
|
174 |
* unblocked the thread. An implementation should document this behavior.
|
jaroslav@1890
|
175 |
*
|
jaroslav@1890
|
176 |
* @since 1.5
|
jaroslav@1890
|
177 |
* @author Doug Lea
|
jaroslav@1890
|
178 |
*/
|
jaroslav@1890
|
179 |
public interface Condition {
|
jaroslav@1890
|
180 |
|
jaroslav@1890
|
181 |
/**
|
jaroslav@1890
|
182 |
* Causes the current thread to wait until it is signalled or
|
jaroslav@1890
|
183 |
* {@linkplain Thread#interrupt interrupted}.
|
jaroslav@1890
|
184 |
*
|
jaroslav@1890
|
185 |
* <p>The lock associated with this {@code Condition} is atomically
|
jaroslav@1890
|
186 |
* released and the current thread becomes disabled for thread scheduling
|
jaroslav@1890
|
187 |
* purposes and lies dormant until <em>one</em> of four things happens:
|
jaroslav@1890
|
188 |
* <ul>
|
jaroslav@1890
|
189 |
* <li>Some other thread invokes the {@link #signal} method for this
|
jaroslav@1890
|
190 |
* {@code Condition} and the current thread happens to be chosen as the
|
jaroslav@1890
|
191 |
* thread to be awakened; or
|
jaroslav@1890
|
192 |
* <li>Some other thread invokes the {@link #signalAll} method for this
|
jaroslav@1890
|
193 |
* {@code Condition}; or
|
jaroslav@1890
|
194 |
* <li>Some other thread {@linkplain Thread#interrupt interrupts} the
|
jaroslav@1890
|
195 |
* current thread, and interruption of thread suspension is supported; or
|
jaroslav@1890
|
196 |
* <li>A "<em>spurious wakeup</em>" occurs.
|
jaroslav@1890
|
197 |
* </ul>
|
jaroslav@1890
|
198 |
*
|
jaroslav@1890
|
199 |
* <p>In all cases, before this method can return the current thread must
|
jaroslav@1890
|
200 |
* re-acquire the lock associated with this condition. When the
|
jaroslav@1890
|
201 |
* thread returns it is <em>guaranteed</em> to hold this lock.
|
jaroslav@1890
|
202 |
*
|
jaroslav@1890
|
203 |
* <p>If the current thread:
|
jaroslav@1890
|
204 |
* <ul>
|
jaroslav@1890
|
205 |
* <li>has its interrupted status set on entry to this method; or
|
jaroslav@1890
|
206 |
* <li>is {@linkplain Thread#interrupt interrupted} while waiting
|
jaroslav@1890
|
207 |
* and interruption of thread suspension is supported,
|
jaroslav@1890
|
208 |
* </ul>
|
jaroslav@1890
|
209 |
* then {@link InterruptedException} is thrown and the current thread's
|
jaroslav@1890
|
210 |
* interrupted status is cleared. It is not specified, in the first
|
jaroslav@1890
|
211 |
* case, whether or not the test for interruption occurs before the lock
|
jaroslav@1890
|
212 |
* is released.
|
jaroslav@1890
|
213 |
*
|
jaroslav@1890
|
214 |
* <p><b>Implementation Considerations</b>
|
jaroslav@1890
|
215 |
*
|
jaroslav@1890
|
216 |
* <p>The current thread is assumed to hold the lock associated with this
|
jaroslav@1890
|
217 |
* {@code Condition} when this method is called.
|
jaroslav@1890
|
218 |
* It is up to the implementation to determine if this is
|
jaroslav@1890
|
219 |
* the case and if not, how to respond. Typically, an exception will be
|
jaroslav@1890
|
220 |
* thrown (such as {@link IllegalMonitorStateException}) and the
|
jaroslav@1890
|
221 |
* implementation must document that fact.
|
jaroslav@1890
|
222 |
*
|
jaroslav@1890
|
223 |
* <p>An implementation can favor responding to an interrupt over normal
|
jaroslav@1890
|
224 |
* method return in response to a signal. In that case the implementation
|
jaroslav@1890
|
225 |
* must ensure that the signal is redirected to another waiting thread, if
|
jaroslav@1890
|
226 |
* there is one.
|
jaroslav@1890
|
227 |
*
|
jaroslav@1890
|
228 |
* @throws InterruptedException if the current thread is interrupted
|
jaroslav@1890
|
229 |
* (and interruption of thread suspension is supported)
|
jaroslav@1890
|
230 |
*/
|
jaroslav@1890
|
231 |
void await() throws InterruptedException;
|
jaroslav@1890
|
232 |
|
jaroslav@1890
|
233 |
/**
|
jaroslav@1890
|
234 |
* Causes the current thread to wait until it is signalled.
|
jaroslav@1890
|
235 |
*
|
jaroslav@1890
|
236 |
* <p>The lock associated with this condition is atomically
|
jaroslav@1890
|
237 |
* released and the current thread becomes disabled for thread scheduling
|
jaroslav@1890
|
238 |
* purposes and lies dormant until <em>one</em> of three things happens:
|
jaroslav@1890
|
239 |
* <ul>
|
jaroslav@1890
|
240 |
* <li>Some other thread invokes the {@link #signal} method for this
|
jaroslav@1890
|
241 |
* {@code Condition} and the current thread happens to be chosen as the
|
jaroslav@1890
|
242 |
* thread to be awakened; or
|
jaroslav@1890
|
243 |
* <li>Some other thread invokes the {@link #signalAll} method for this
|
jaroslav@1890
|
244 |
* {@code Condition}; or
|
jaroslav@1890
|
245 |
* <li>A "<em>spurious wakeup</em>" occurs.
|
jaroslav@1890
|
246 |
* </ul>
|
jaroslav@1890
|
247 |
*
|
jaroslav@1890
|
248 |
* <p>In all cases, before this method can return the current thread must
|
jaroslav@1890
|
249 |
* re-acquire the lock associated with this condition. When the
|
jaroslav@1890
|
250 |
* thread returns it is <em>guaranteed</em> to hold this lock.
|
jaroslav@1890
|
251 |
*
|
jaroslav@1890
|
252 |
* <p>If the current thread's interrupted status is set when it enters
|
jaroslav@1890
|
253 |
* this method, or it is {@linkplain Thread#interrupt interrupted}
|
jaroslav@1890
|
254 |
* while waiting, it will continue to wait until signalled. When it finally
|
jaroslav@1890
|
255 |
* returns from this method its interrupted status will still
|
jaroslav@1890
|
256 |
* be set.
|
jaroslav@1890
|
257 |
*
|
jaroslav@1890
|
258 |
* <p><b>Implementation Considerations</b>
|
jaroslav@1890
|
259 |
*
|
jaroslav@1890
|
260 |
* <p>The current thread is assumed to hold the lock associated with this
|
jaroslav@1890
|
261 |
* {@code Condition} when this method is called.
|
jaroslav@1890
|
262 |
* It is up to the implementation to determine if this is
|
jaroslav@1890
|
263 |
* the case and if not, how to respond. Typically, an exception will be
|
jaroslav@1890
|
264 |
* thrown (such as {@link IllegalMonitorStateException}) and the
|
jaroslav@1890
|
265 |
* implementation must document that fact.
|
jaroslav@1890
|
266 |
*/
|
jaroslav@1890
|
267 |
void awaitUninterruptibly();
|
jaroslav@1890
|
268 |
|
jaroslav@1890
|
269 |
/**
|
jaroslav@1890
|
270 |
* Causes the current thread to wait until it is signalled or interrupted,
|
jaroslav@1890
|
271 |
* or the specified waiting time elapses.
|
jaroslav@1890
|
272 |
*
|
jaroslav@1890
|
273 |
* <p>The lock associated with this condition is atomically
|
jaroslav@1890
|
274 |
* released and the current thread becomes disabled for thread scheduling
|
jaroslav@1890
|
275 |
* purposes and lies dormant until <em>one</em> of five things happens:
|
jaroslav@1890
|
276 |
* <ul>
|
jaroslav@1890
|
277 |
* <li>Some other thread invokes the {@link #signal} method for this
|
jaroslav@1890
|
278 |
* {@code Condition} and the current thread happens to be chosen as the
|
jaroslav@1890
|
279 |
* thread to be awakened; or
|
jaroslav@1890
|
280 |
* <li>Some other thread invokes the {@link #signalAll} method for this
|
jaroslav@1890
|
281 |
* {@code Condition}; or
|
jaroslav@1890
|
282 |
* <li>Some other thread {@linkplain Thread#interrupt interrupts} the
|
jaroslav@1890
|
283 |
* current thread, and interruption of thread suspension is supported; or
|
jaroslav@1890
|
284 |
* <li>The specified waiting time elapses; or
|
jaroslav@1890
|
285 |
* <li>A "<em>spurious wakeup</em>" occurs.
|
jaroslav@1890
|
286 |
* </ul>
|
jaroslav@1890
|
287 |
*
|
jaroslav@1890
|
288 |
* <p>In all cases, before this method can return the current thread must
|
jaroslav@1890
|
289 |
* re-acquire the lock associated with this condition. When the
|
jaroslav@1890
|
290 |
* thread returns it is <em>guaranteed</em> to hold this lock.
|
jaroslav@1890
|
291 |
*
|
jaroslav@1890
|
292 |
* <p>If the current thread:
|
jaroslav@1890
|
293 |
* <ul>
|
jaroslav@1890
|
294 |
* <li>has its interrupted status set on entry to this method; or
|
jaroslav@1890
|
295 |
* <li>is {@linkplain Thread#interrupt interrupted} while waiting
|
jaroslav@1890
|
296 |
* and interruption of thread suspension is supported,
|
jaroslav@1890
|
297 |
* </ul>
|
jaroslav@1890
|
298 |
* then {@link InterruptedException} is thrown and the current thread's
|
jaroslav@1890
|
299 |
* interrupted status is cleared. It is not specified, in the first
|
jaroslav@1890
|
300 |
* case, whether or not the test for interruption occurs before the lock
|
jaroslav@1890
|
301 |
* is released.
|
jaroslav@1890
|
302 |
*
|
jaroslav@1890
|
303 |
* <p>The method returns an estimate of the number of nanoseconds
|
jaroslav@1890
|
304 |
* remaining to wait given the supplied {@code nanosTimeout}
|
jaroslav@1890
|
305 |
* value upon return, or a value less than or equal to zero if it
|
jaroslav@1890
|
306 |
* timed out. This value can be used to determine whether and how
|
jaroslav@1890
|
307 |
* long to re-wait in cases where the wait returns but an awaited
|
jaroslav@1890
|
308 |
* condition still does not hold. Typical uses of this method take
|
jaroslav@1890
|
309 |
* the following form:
|
jaroslav@1890
|
310 |
*
|
jaroslav@1890
|
311 |
* <pre> {@code
|
jaroslav@1890
|
312 |
* boolean aMethod(long timeout, TimeUnit unit) {
|
jaroslav@1890
|
313 |
* long nanos = unit.toNanos(timeout);
|
jaroslav@1890
|
314 |
* lock.lock();
|
jaroslav@1890
|
315 |
* try {
|
jaroslav@1890
|
316 |
* while (!conditionBeingWaitedFor()) {
|
jaroslav@1890
|
317 |
* if (nanos <= 0L)
|
jaroslav@1890
|
318 |
* return false;
|
jaroslav@1890
|
319 |
* nanos = theCondition.awaitNanos(nanos);
|
jaroslav@1890
|
320 |
* }
|
jaroslav@1890
|
321 |
* // ...
|
jaroslav@1890
|
322 |
* } finally {
|
jaroslav@1890
|
323 |
* lock.unlock();
|
jaroslav@1890
|
324 |
* }
|
jaroslav@1890
|
325 |
* }}</pre>
|
jaroslav@1890
|
326 |
*
|
jaroslav@1890
|
327 |
* <p> Design note: This method requires a nanosecond argument so
|
jaroslav@1890
|
328 |
* as to avoid truncation errors in reporting remaining times.
|
jaroslav@1890
|
329 |
* Such precision loss would make it difficult for programmers to
|
jaroslav@1890
|
330 |
* ensure that total waiting times are not systematically shorter
|
jaroslav@1890
|
331 |
* than specified when re-waits occur.
|
jaroslav@1890
|
332 |
*
|
jaroslav@1890
|
333 |
* <p><b>Implementation Considerations</b>
|
jaroslav@1890
|
334 |
*
|
jaroslav@1890
|
335 |
* <p>The current thread is assumed to hold the lock associated with this
|
jaroslav@1890
|
336 |
* {@code Condition} when this method is called.
|
jaroslav@1890
|
337 |
* It is up to the implementation to determine if this is
|
jaroslav@1890
|
338 |
* the case and if not, how to respond. Typically, an exception will be
|
jaroslav@1890
|
339 |
* thrown (such as {@link IllegalMonitorStateException}) and the
|
jaroslav@1890
|
340 |
* implementation must document that fact.
|
jaroslav@1890
|
341 |
*
|
jaroslav@1890
|
342 |
* <p>An implementation can favor responding to an interrupt over normal
|
jaroslav@1890
|
343 |
* method return in response to a signal, or over indicating the elapse
|
jaroslav@1890
|
344 |
* of the specified waiting time. In either case the implementation
|
jaroslav@1890
|
345 |
* must ensure that the signal is redirected to another waiting thread, if
|
jaroslav@1890
|
346 |
* there is one.
|
jaroslav@1890
|
347 |
*
|
jaroslav@1890
|
348 |
* @param nanosTimeout the maximum time to wait, in nanoseconds
|
jaroslav@1890
|
349 |
* @return an estimate of the {@code nanosTimeout} value minus
|
jaroslav@1890
|
350 |
* the time spent waiting upon return from this method.
|
jaroslav@1890
|
351 |
* A positive value may be used as the argument to a
|
jaroslav@1890
|
352 |
* subsequent call to this method to finish waiting out
|
jaroslav@1890
|
353 |
* the desired time. A value less than or equal to zero
|
jaroslav@1890
|
354 |
* indicates that no time remains.
|
jaroslav@1890
|
355 |
* @throws InterruptedException if the current thread is interrupted
|
jaroslav@1890
|
356 |
* (and interruption of thread suspension is supported)
|
jaroslav@1890
|
357 |
*/
|
jaroslav@1890
|
358 |
long awaitNanos(long nanosTimeout) throws InterruptedException;
|
jaroslav@1890
|
359 |
|
jaroslav@1890
|
360 |
/**
|
jaroslav@1890
|
361 |
* Causes the current thread to wait until it is signalled or interrupted,
|
jaroslav@1890
|
362 |
* or the specified waiting time elapses. This method is behaviorally
|
jaroslav@1890
|
363 |
* equivalent to:<br>
|
jaroslav@1890
|
364 |
* <pre>
|
jaroslav@1890
|
365 |
* awaitNanos(unit.toNanos(time)) > 0
|
jaroslav@1890
|
366 |
* </pre>
|
jaroslav@1890
|
367 |
* @param time the maximum time to wait
|
jaroslav@1890
|
368 |
* @param unit the time unit of the {@code time} argument
|
jaroslav@1890
|
369 |
* @return {@code false} if the waiting time detectably elapsed
|
jaroslav@1890
|
370 |
* before return from the method, else {@code true}
|
jaroslav@1890
|
371 |
* @throws InterruptedException if the current thread is interrupted
|
jaroslav@1890
|
372 |
* (and interruption of thread suspension is supported)
|
jaroslav@1890
|
373 |
*/
|
jaroslav@1890
|
374 |
boolean await(long time, TimeUnit unit) throws InterruptedException;
|
jaroslav@1890
|
375 |
|
jaroslav@1890
|
376 |
/**
|
jaroslav@1890
|
377 |
* Causes the current thread to wait until it is signalled or interrupted,
|
jaroslav@1890
|
378 |
* or the specified deadline elapses.
|
jaroslav@1890
|
379 |
*
|
jaroslav@1890
|
380 |
* <p>The lock associated with this condition is atomically
|
jaroslav@1890
|
381 |
* released and the current thread becomes disabled for thread scheduling
|
jaroslav@1890
|
382 |
* purposes and lies dormant until <em>one</em> of five things happens:
|
jaroslav@1890
|
383 |
* <ul>
|
jaroslav@1890
|
384 |
* <li>Some other thread invokes the {@link #signal} method for this
|
jaroslav@1890
|
385 |
* {@code Condition} and the current thread happens to be chosen as the
|
jaroslav@1890
|
386 |
* thread to be awakened; or
|
jaroslav@1890
|
387 |
* <li>Some other thread invokes the {@link #signalAll} method for this
|
jaroslav@1890
|
388 |
* {@code Condition}; or
|
jaroslav@1890
|
389 |
* <li>Some other thread {@linkplain Thread#interrupt interrupts} the
|
jaroslav@1890
|
390 |
* current thread, and interruption of thread suspension is supported; or
|
jaroslav@1890
|
391 |
* <li>The specified deadline elapses; or
|
jaroslav@1890
|
392 |
* <li>A "<em>spurious wakeup</em>" occurs.
|
jaroslav@1890
|
393 |
* </ul>
|
jaroslav@1890
|
394 |
*
|
jaroslav@1890
|
395 |
* <p>In all cases, before this method can return the current thread must
|
jaroslav@1890
|
396 |
* re-acquire the lock associated with this condition. When the
|
jaroslav@1890
|
397 |
* thread returns it is <em>guaranteed</em> to hold this lock.
|
jaroslav@1890
|
398 |
*
|
jaroslav@1890
|
399 |
*
|
jaroslav@1890
|
400 |
* <p>If the current thread:
|
jaroslav@1890
|
401 |
* <ul>
|
jaroslav@1890
|
402 |
* <li>has its interrupted status set on entry to this method; or
|
jaroslav@1890
|
403 |
* <li>is {@linkplain Thread#interrupt interrupted} while waiting
|
jaroslav@1890
|
404 |
* and interruption of thread suspension is supported,
|
jaroslav@1890
|
405 |
* </ul>
|
jaroslav@1890
|
406 |
* then {@link InterruptedException} is thrown and the current thread's
|
jaroslav@1890
|
407 |
* interrupted status is cleared. It is not specified, in the first
|
jaroslav@1890
|
408 |
* case, whether or not the test for interruption occurs before the lock
|
jaroslav@1890
|
409 |
* is released.
|
jaroslav@1890
|
410 |
*
|
jaroslav@1890
|
411 |
*
|
jaroslav@1890
|
412 |
* <p>The return value indicates whether the deadline has elapsed,
|
jaroslav@1890
|
413 |
* which can be used as follows:
|
jaroslav@1890
|
414 |
* <pre> {@code
|
jaroslav@1890
|
415 |
* boolean aMethod(Date deadline) {
|
jaroslav@1890
|
416 |
* boolean stillWaiting = true;
|
jaroslav@1890
|
417 |
* lock.lock();
|
jaroslav@1890
|
418 |
* try {
|
jaroslav@1890
|
419 |
* while (!conditionBeingWaitedFor()) {
|
jaroslav@1890
|
420 |
* if (!stillWaiting)
|
jaroslav@1890
|
421 |
* return false;
|
jaroslav@1890
|
422 |
* stillWaiting = theCondition.awaitUntil(deadline);
|
jaroslav@1890
|
423 |
* }
|
jaroslav@1890
|
424 |
* // ...
|
jaroslav@1890
|
425 |
* } finally {
|
jaroslav@1890
|
426 |
* lock.unlock();
|
jaroslav@1890
|
427 |
* }
|
jaroslav@1890
|
428 |
* }}</pre>
|
jaroslav@1890
|
429 |
*
|
jaroslav@1890
|
430 |
* <p><b>Implementation Considerations</b>
|
jaroslav@1890
|
431 |
*
|
jaroslav@1890
|
432 |
* <p>The current thread is assumed to hold the lock associated with this
|
jaroslav@1890
|
433 |
* {@code Condition} when this method is called.
|
jaroslav@1890
|
434 |
* It is up to the implementation to determine if this is
|
jaroslav@1890
|
435 |
* the case and if not, how to respond. Typically, an exception will be
|
jaroslav@1890
|
436 |
* thrown (such as {@link IllegalMonitorStateException}) and the
|
jaroslav@1890
|
437 |
* implementation must document that fact.
|
jaroslav@1890
|
438 |
*
|
jaroslav@1890
|
439 |
* <p>An implementation can favor responding to an interrupt over normal
|
jaroslav@1890
|
440 |
* method return in response to a signal, or over indicating the passing
|
jaroslav@1890
|
441 |
* of the specified deadline. In either case the implementation
|
jaroslav@1890
|
442 |
* must ensure that the signal is redirected to another waiting thread, if
|
jaroslav@1890
|
443 |
* there is one.
|
jaroslav@1890
|
444 |
*
|
jaroslav@1890
|
445 |
* @param deadline the absolute time to wait until
|
jaroslav@1890
|
446 |
* @return {@code false} if the deadline has elapsed upon return, else
|
jaroslav@1890
|
447 |
* {@code true}
|
jaroslav@1890
|
448 |
* @throws InterruptedException if the current thread is interrupted
|
jaroslav@1890
|
449 |
* (and interruption of thread suspension is supported)
|
jaroslav@1890
|
450 |
*/
|
jaroslav@1890
|
451 |
boolean awaitUntil(Date deadline) throws InterruptedException;
|
jaroslav@1890
|
452 |
|
jaroslav@1890
|
453 |
/**
|
jaroslav@1890
|
454 |
* Wakes up one waiting thread.
|
jaroslav@1890
|
455 |
*
|
jaroslav@1890
|
456 |
* <p>If any threads are waiting on this condition then one
|
jaroslav@1890
|
457 |
* is selected for waking up. That thread must then re-acquire the
|
jaroslav@1890
|
458 |
* lock before returning from {@code await}.
|
jaroslav@1890
|
459 |
*
|
jaroslav@1890
|
460 |
* <p><b>Implementation Considerations</b>
|
jaroslav@1890
|
461 |
*
|
jaroslav@1890
|
462 |
* <p>An implementation may (and typically does) require that the
|
jaroslav@1890
|
463 |
* current thread hold the lock associated with this {@code
|
jaroslav@1890
|
464 |
* Condition} when this method is called. Implementations must
|
jaroslav@1890
|
465 |
* document this precondition and any actions taken if the lock is
|
jaroslav@1890
|
466 |
* not held. Typically, an exception such as {@link
|
jaroslav@1890
|
467 |
* IllegalMonitorStateException} will be thrown.
|
jaroslav@1890
|
468 |
*/
|
jaroslav@1890
|
469 |
void signal();
|
jaroslav@1890
|
470 |
|
jaroslav@1890
|
471 |
/**
|
jaroslav@1890
|
472 |
* Wakes up all waiting threads.
|
jaroslav@1890
|
473 |
*
|
jaroslav@1890
|
474 |
* <p>If any threads are waiting on this condition then they are
|
jaroslav@1890
|
475 |
* all woken up. Each thread must re-acquire the lock before it can
|
jaroslav@1890
|
476 |
* return from {@code await}.
|
jaroslav@1890
|
477 |
*
|
jaroslav@1890
|
478 |
* <p><b>Implementation Considerations</b>
|
jaroslav@1890
|
479 |
*
|
jaroslav@1890
|
480 |
* <p>An implementation may (and typically does) require that the
|
jaroslav@1890
|
481 |
* current thread hold the lock associated with this {@code
|
jaroslav@1890
|
482 |
* Condition} when this method is called. Implementations must
|
jaroslav@1890
|
483 |
* document this precondition and any actions taken if the lock is
|
jaroslav@1890
|
484 |
* not held. Typically, an exception such as {@link
|
jaroslav@1890
|
485 |
* IllegalMonitorStateException} will be thrown.
|
jaroslav@1890
|
486 |
*/
|
jaroslav@1890
|
487 |
void signalAll();
|
jaroslav@1890
|
488 |
}
|