0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 0N/A * This code is free software; you can redistribute it and/or modify it 0N/A * under the terms of the GNU General Public License version 2 only, as 2362N/A * published by the Free Software Foundation. Oracle designates this 0N/A * particular file as subject to the "Classpath" exception as provided 2362N/A * by Oracle in the LICENSE file that accompanied this code. 0N/A * This code is distributed in the hope that it will be useful, but WITHOUT 0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 0N/A * version 2 for more details (a copy is included in the LICENSE file that 0N/A * accompanied this code). 0N/A * You should have received a copy of the GNU General Public License version 0N/A * 2 along with this work; if not, write to the Free Software Foundation, 0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 2362N/A * or visit www.oracle.com if you need additional information or have any 0N/A * This file is available under and governed by the GNU General Public 0N/A * License version 2 only, as published by the Free Software Foundation. 0N/A * However, the following notice accompanied the original version of this 0N/A * Written by Doug Lea with assistance from members of JCP JSR-166 0N/A * Expert Group and released to the public domain, as explained at 0N/A * {@code Condition} factors out the {@code Object} monitor 0N/A * methods ({@link Object#wait() wait}, {@link Object#notify notify} 0N/A * and {@link Object#notifyAll notifyAll}) into distinct objects to 0N/A * give the effect of having multiple wait-sets per object, by 0N/A * combining them with the use of arbitrary {@link Lock} implementations. 0N/A * Where a {@code Lock} replaces the use of {@code synchronized} methods 0N/A * and statements, a {@code Condition} replaces the use of the Object 0N/A * <p>Conditions (also known as <em>condition queues</em> or 0N/A * <em>condition variables</em>) provide a means for one thread to 0N/A * suspend execution (to "wait") until notified by another 0N/A * thread that some state condition may now be true. Because access 0N/A * to this shared state information occurs in different threads, it 0N/A * must be protected, so a lock of some form is associated with the 0N/A * condition. The key property that waiting for a condition provides 0N/A * is that it <em>atomically</em> releases the associated lock and 0N/A * suspends the current thread, just like {@code Object.wait}. 0N/A * <p>A {@code Condition} instance is intrinsically bound to a lock. 0N/A * To obtain a {@code Condition} instance for a particular {@link Lock} 0N/A * instance use its {@link Lock#newCondition newCondition()} method. 0N/A * <p>As an example, suppose we have a bounded buffer which supports 0N/A * {@code put} and {@code take} methods. If a 0N/A * {@code take} is attempted on an empty buffer, then the thread will block 0N/A * until an item becomes available; if a {@code put} is attempted on a 0N/A * full buffer, then the thread will block until a space becomes available. 0N/A * We would like to keep waiting {@code put} threads and {@code take} 0N/A * threads in separate wait-sets so that we can use the optimization of 0N/A * only notifying a single thread at a time when items or spaces become 0N/A * available in the buffer. This can be achieved using two 0N/A * {@link Condition} instances. 0N/A * class BoundedBuffer { 0N/A * <b>final Lock lock = new ReentrantLock();</b> 0N/A * final Condition notFull = <b>lock.newCondition(); </b> 0N/A * final Condition notEmpty = <b>lock.newCondition(); </b> 0N/A * final Object[] items = new Object[100]; 0N/A * int putptr, takeptr, count; 0N/A * public void put(Object x) throws InterruptedException { 0N/A * while (count == items.length) 0N/A * <b>notFull.await();</b> 0N/A * items[putptr] = x; 0N/A * if (++putptr == items.length) putptr = 0; 0N/A * <b>notEmpty.signal();</b> 0N/A * public Object take() throws InterruptedException { 0N/A * while (count == 0) 0N/A * <b>notEmpty.await();</b> 0N/A * Object x = items[takeptr]; 0N/A * if (++takeptr == items.length) takeptr = 0; 0N/A * <b>notFull.signal();</b> 0N/A * (The {@link java.util.concurrent.ArrayBlockingQueue} class provides 0N/A * this functionality, so there is no reason to implement this 0N/A * sample usage class.) 0N/A * <p>A {@code Condition} implementation can provide behavior and semantics 0N/A * different from that of the {@code Object} monitor methods, such as 0N/A * guaranteed ordering for notifications, or not requiring a lock to be held 0N/A * when performing notifications. 0N/A * If an implementation provides such specialized semantics then the 0N/A * implementation must document those semantics. 0N/A * <p>Note that {@code Condition} instances are just normal objects and can 0N/A * themselves be used as the target in a {@code synchronized} statement, 0N/A * and can have their own monitor {@link Object#wait wait} and 0N/A * {@link Object#notify notification} methods invoked. 0N/A * Acquiring the monitor lock of a {@code Condition} instance, or using its 0N/A * monitor methods, has no specified relationship with acquiring the 0N/A * {@link Lock} associated with that {@code Condition} or the use of its 0N/A * {@linkplain #await waiting} and {@linkplain #signal signalling} methods. 0N/A * It is recommended that to avoid confusion you never use {@code Condition} 0N/A * instances in this way, except perhaps within their own implementation. 0N/A * <p>Except where noted, passing a {@code null} value for any parameter 0N/A * will result in a {@link NullPointerException} being thrown. 0N/A * <h3>Implementation Considerations</h3> 0N/A * <p>When waiting upon a {@code Condition}, a "<em>spurious 0N/A * wakeup</em>" is permitted to occur, in 0N/A * general, as a concession to the underlying platform semantics. 0N/A * This has little practical impact on most application programs as a 0N/A * {@code Condition} should always be waited upon in a loop, testing 0N/A * the state predicate that is being waited for. An implementation is 0N/A * free to remove the possibility of spurious wakeups but it is 0N/A * recommended that applications programmers always assume that they can 0N/A * occur and so always wait in a loop. 0N/A * <p>The three forms of condition waiting 0N/A * (interruptible, non-interruptible, and timed) may differ in their ease of 0N/A * implementation on some platforms and in their performance characteristics. 0N/A * In particular, it may be difficult to provide these features and maintain 0N/A * specific semantics such as ordering guarantees. 0N/A * Further, the ability to interrupt the actual suspension of the thread may 0N/A * not always be feasible to implement on all platforms. 0N/A * <p>Consequently, an implementation is not required to define exactly the 0N/A * same guarantees or semantics for all three forms of waiting, nor is it 0N/A * required to support interruption of the actual suspension of the thread. 0N/A * <p>An implementation is required to 0N/A * clearly document the semantics and guarantees provided by each of the 0N/A * waiting methods, and when an implementation does support interruption of 0N/A * thread suspension then it must obey the interruption semantics as defined 0N/A * in this interface. 0N/A * <p>As interruption generally implies cancellation, and checks for 0N/A * interruption are often infrequent, an implementation can favor responding 0N/A * to an interrupt over normal method return. This is true even if it can be 1771N/A * shown that the interrupt occurred after another action that may have 1771N/A * unblocked the thread. An implementation should document this behavior. 0N/A * Causes the current thread to wait until it is signalled or 0N/A * {@linkplain Thread#interrupt interrupted}. 0N/A * <p>The lock associated with this {@code Condition} is atomically 0N/A * released and the current thread becomes disabled for thread scheduling 0N/A * purposes and lies dormant until <em>one</em> of four things happens: 0N/A * <li>Some other thread invokes the {@link #signal} method for this 0N/A * {@code Condition} and the current thread happens to be chosen as the 0N/A * thread to be awakened; or 0N/A * <li>Some other thread invokes the {@link #signalAll} method for this 0N/A * {@code Condition}; or 0N/A * <li>Some other thread {@linkplain Thread#interrupt interrupts} the 0N/A * current thread, and interruption of thread suspension is supported; or 0N/A * <li>A "<em>spurious wakeup</em>" occurs. 0N/A * <p>In all cases, before this method can return the current thread must 0N/A * re-acquire the lock associated with this condition. When the 0N/A * thread returns it is <em>guaranteed</em> to hold this lock. 0N/A * <p>If the current thread: 0N/A * <li>has its interrupted status set on entry to this method; or 0N/A * <li>is {@linkplain Thread#interrupt interrupted} while waiting 0N/A * and interruption of thread suspension is supported, 0N/A * then {@link InterruptedException} is thrown and the current thread's 0N/A * interrupted status is cleared. It is not specified, in the first 0N/A * case, whether or not the test for interruption occurs before the lock 0N/A * <p><b>Implementation Considerations</b> 0N/A * <p>The current thread is assumed to hold the lock associated with this 0N/A * {@code Condition} when this method is called. 0N/A * It is up to the implementation to determine if this is 0N/A * the case and if not, how to respond. Typically, an exception will be 0N/A * thrown (such as {@link IllegalMonitorStateException}) and the 0N/A * implementation must document that fact. 0N/A * <p>An implementation can favor responding to an interrupt over normal 0N/A * method return in response to a signal. In that case the implementation 0N/A * must ensure that the signal is redirected to another waiting thread, if 0N/A * @throws InterruptedException if the current thread is interrupted 0N/A * (and interruption of thread suspension is supported) 0N/A * Causes the current thread to wait until it is signalled. 0N/A * <p>The lock associated with this condition is atomically 0N/A * released and the current thread becomes disabled for thread scheduling 0N/A * purposes and lies dormant until <em>one</em> of three things happens: 0N/A * <li>Some other thread invokes the {@link #signal} method for this 0N/A * {@code Condition} and the current thread happens to be chosen as the 0N/A * thread to be awakened; or 0N/A * <li>Some other thread invokes the {@link #signalAll} method for this 0N/A * {@code Condition}; or 0N/A * <li>A "<em>spurious wakeup</em>" occurs. 0N/A * <p>In all cases, before this method can return the current thread must 0N/A * re-acquire the lock associated with this condition. When the 0N/A * thread returns it is <em>guaranteed</em> to hold this lock. 0N/A * <p>If the current thread's interrupted status is set when it enters 0N/A * this method, or it is {@linkplain Thread#interrupt interrupted} 0N/A * while waiting, it will continue to wait until signalled. When it finally 0N/A * returns from this method its interrupted status will still 0N/A * <p><b>Implementation Considerations</b> 0N/A * <p>The current thread is assumed to hold the lock associated with this 0N/A * {@code Condition} when this method is called. 0N/A * It is up to the implementation to determine if this is 0N/A * the case and if not, how to respond. Typically, an exception will be 0N/A * thrown (such as {@link IllegalMonitorStateException}) and the 0N/A * implementation must document that fact. 0N/A * Causes the current thread to wait until it is signalled or interrupted, 0N/A * or the specified waiting time elapses. 0N/A * <p>The lock associated with this condition is atomically 0N/A * released and the current thread becomes disabled for thread scheduling 0N/A * purposes and lies dormant until <em>one</em> of five things happens: 0N/A * <li>Some other thread invokes the {@link #signal} method for this 0N/A * {@code Condition} and the current thread happens to be chosen as the 0N/A * thread to be awakened; or 0N/A * <li>Some other thread invokes the {@link #signalAll} method for this 0N/A * {@code Condition}; or 0N/A * <li>Some other thread {@linkplain Thread#interrupt interrupts} the 0N/A * current thread, and interruption of thread suspension is supported; or 0N/A * <li>The specified waiting time elapses; or 0N/A * <li>A "<em>spurious wakeup</em>" occurs. 0N/A * <p>In all cases, before this method can return the current thread must 0N/A * re-acquire the lock associated with this condition. When the 0N/A * thread returns it is <em>guaranteed</em> to hold this lock. 0N/A * <p>If the current thread: 0N/A * <li>has its interrupted status set on entry to this method; or 0N/A * <li>is {@linkplain Thread#interrupt interrupted} while waiting 0N/A * and interruption of thread suspension is supported, 0N/A * then {@link InterruptedException} is thrown and the current thread's 0N/A * interrupted status is cleared. It is not specified, in the first 0N/A * case, whether or not the test for interruption occurs before the lock 0N/A * <p>The method returns an estimate of the number of nanoseconds 0N/A * remaining to wait given the supplied {@code nanosTimeout} 0N/A * value upon return, or a value less than or equal to zero if it 0N/A * timed out. This value can be used to determine whether and how 0N/A * long to re-wait in cases where the wait returns but an awaited 0N/A * condition still does not hold. Typical uses of this method take 0N/A * the following form: 3387N/A * boolean aMethod(long timeout, TimeUnit unit) { 3387N/A * long nanos = unit.toNanos(timeout); 3387N/A * while (!conditionBeingWaitedFor()) { 3387N/A * nanos = theCondition.awaitNanos(nanos); 0N/A * <p> Design note: This method requires a nanosecond argument so 0N/A * as to avoid truncation errors in reporting remaining times. 0N/A * Such precision loss would make it difficult for programmers to 0N/A * ensure that total waiting times are not systematically shorter 0N/A * than specified when re-waits occur. 0N/A * <p><b>Implementation Considerations</b> 0N/A * <p>The current thread is assumed to hold the lock associated with this 0N/A * {@code Condition} when this method is called. 0N/A * It is up to the implementation to determine if this is 0N/A * the case and if not, how to respond. Typically, an exception will be 0N/A * thrown (such as {@link IllegalMonitorStateException}) and the 0N/A * implementation must document that fact. 0N/A * <p>An implementation can favor responding to an interrupt over normal 0N/A * method return in response to a signal, or over indicating the elapse 0N/A * of the specified waiting time. In either case the implementation 0N/A * must ensure that the signal is redirected to another waiting thread, if 0N/A * @param nanosTimeout the maximum time to wait, in nanoseconds 0N/A * @return an estimate of the {@code nanosTimeout} value minus 0N/A * the time spent waiting upon return from this method. 0N/A * A positive value may be used as the argument to a 0N/A * subsequent call to this method to finish waiting out 0N/A * the desired time. A value less than or equal to zero 0N/A * indicates that no time remains. 0N/A * @throws InterruptedException if the current thread is interrupted 0N/A * (and interruption of thread suspension is supported) 0N/A * Causes the current thread to wait until it is signalled or interrupted, 0N/A * or the specified waiting time elapses. This method is behaviorally 0N/A * equivalent to:<br> 0N/A * awaitNanos(unit.toNanos(time)) > 0 0N/A * @param time the maximum time to wait 0N/A * @param unit the time unit of the {@code time} argument 0N/A * @return {@code false} if the waiting time detectably elapsed 0N/A * before return from the method, else {@code true} 0N/A * @throws InterruptedException if the current thread is interrupted 0N/A * (and interruption of thread suspension is supported) 0N/A * Causes the current thread to wait until it is signalled or interrupted, 0N/A * or the specified deadline elapses. 0N/A * <p>The lock associated with this condition is atomically 0N/A * released and the current thread becomes disabled for thread scheduling 0N/A * purposes and lies dormant until <em>one</em> of five things happens: 0N/A * <li>Some other thread invokes the {@link #signal} method for this 0N/A * {@code Condition} and the current thread happens to be chosen as the 0N/A * thread to be awakened; or 0N/A * <li>Some other thread invokes the {@link #signalAll} method for this 0N/A * {@code Condition}; or 0N/A * <li>Some other thread {@linkplain Thread#interrupt interrupts} the 0N/A * current thread, and interruption of thread suspension is supported; or 0N/A * <li>The specified deadline elapses; or 0N/A * <li>A "<em>spurious wakeup</em>" occurs. 0N/A * <p>In all cases, before this method can return the current thread must 0N/A * re-acquire the lock associated with this condition. When the 0N/A * thread returns it is <em>guaranteed</em> to hold this lock. 0N/A * <p>If the current thread: 0N/A * <li>has its interrupted status set on entry to this method; or 0N/A * <li>is {@linkplain Thread#interrupt interrupted} while waiting 0N/A * and interruption of thread suspension is supported, 0N/A * then {@link InterruptedException} is thrown and the current thread's 0N/A * interrupted status is cleared. It is not specified, in the first 0N/A * case, whether or not the test for interruption occurs before the lock 0N/A * <p>The return value indicates whether the deadline has elapsed, 0N/A * which can be used as follows: 3387N/A * boolean aMethod(Date deadline) { 0N/A * boolean stillWaiting = true; 3387N/A * while (!conditionBeingWaitedFor()) { 3387N/A * stillWaiting = theCondition.awaitUntil(deadline); 0N/A * <p><b>Implementation Considerations</b> 0N/A * <p>The current thread is assumed to hold the lock associated with this 0N/A * {@code Condition} when this method is called. 0N/A * It is up to the implementation to determine if this is 0N/A * the case and if not, how to respond. Typically, an exception will be 0N/A * thrown (such as {@link IllegalMonitorStateException}) and the 0N/A * implementation must document that fact. 0N/A * <p>An implementation can favor responding to an interrupt over normal 0N/A * method return in response to a signal, or over indicating the passing 0N/A * of the specified deadline. In either case the implementation 0N/A * must ensure that the signal is redirected to another waiting thread, if 0N/A * @param deadline the absolute time to wait until 0N/A * @return {@code false} if the deadline has elapsed upon return, else 0N/A * @throws InterruptedException if the current thread is interrupted 0N/A * (and interruption of thread suspension is supported) 0N/A * Wakes up one waiting thread. 0N/A * <p>If any threads are waiting on this condition then one 0N/A * is selected for waking up. That thread must then re-acquire the 0N/A * lock before returning from {@code await}. 3387N/A * <p><b>Implementation Considerations</b> 3387N/A * <p>An implementation may (and typically does) require that the 3387N/A * current thread hold the lock associated with this {@code 3387N/A * Condition} when this method is called. Implementations must 3387N/A * document this precondition and any actions taken if the lock is 3387N/A * not held. Typically, an exception such as {@link 3387N/A * IllegalMonitorStateException} will be thrown. 0N/A * Wakes up all waiting threads. 0N/A * <p>If any threads are waiting on this condition then they are 0N/A * all woken up. Each thread must re-acquire the lock before it can 0N/A * return from {@code await}. 3387N/A * <p><b>Implementation Considerations</b> 3387N/A * <p>An implementation may (and typically does) require that the 3387N/A * current thread hold the lock associated with this {@code 3387N/A * Condition} when this method is called. Implementations must 3387N/A * document this precondition and any actions taken if the lock is 3387N/A * not held. Typically, an exception such as {@link 3387N/A * IllegalMonitorStateException} will be thrown.