Inheritance diagram for Condition:
Public Member Functions | |
void | wait () |
long | waitNanos (long nanosTimeout) |
bool | wait (long time, TimeUnit unit) |
void | notify () |
void | notifyAll () |
Because access to this shared state information occurs in different threads, it must be protected, so a lock of some form is associated with the condition. The key property that waiting for a condition provides is that it atomically releases the associated lock and suspends the current thread.
A Condition
instance is intrinsically bound to a lock. To obtain a Condition
instance for a particular Lock instance use its newCondition() method.
A Condition
implementation can provide customized behavior and semantics such as guaranteed ordering for notifications, or not requiring a lock to be held when performing notifications. If an implementation provides such specialized semantics then the implementation must document those semantics.
Note that Condition
instances are just normal objects and can themselves be used as the target in a synchronized
statement. Acquiring the monitor lock of a Condition
instance has no specified relationship with acquiring the Lock associated with that Condition
. It is recommended that to avoid confusion you never use Condition
instances in this way, except perhaps within their own implementation.
When waiting upon a Condition
, a "spurious wakeup" is permitted to occur, in general, as a concession to the underlying platform semantics. This has little practical impact on most application programs as a Condition
should always be waited upon in a loop, testing the state predicate that is being waited for. An implementation is free to remove the possibility of spurious wakeups but it is recommended that applications programmers always assume that they can occur and so always wait in a loop.
Definition at line 60 of file Condition.d.
|
Causes the current thread to wait until it is notified.
The lock associated with this
In all cases, before this method can return the current thread must re-acquire the lock associated with this condition. When the thread returns it is guaranteed to hold this lock. Implementation Considerations
The current thread is assumed to hold the lock associated with this Reimplemented in AbstractLock::ConditionObject. Referenced by CyclicBarrier::dowait(). |
|
Causes the current thread to wait until it is notified or the specified waiting time elapses. The lock associated with this condition is atomically released and the current thread becomes disabled for thread scheduling purposes and lies dormant until one of four things happens:
In all cases, before this method can return the current thread must re-acquire the lock associated with this condition. When the thread returns it is guaranteed to hold this lock.
The method returns an estimate of the number of nanoseconds remaining to wait given the supplied
synchronized bool aMethod(long timeout, TimeUnit unit) { long nanosTimeout = unit.toNanos(timeout); while (!conditionBeingWaitedFor) { if (nanosTimeout > 0) nanosTimeout = theCondition.waitNanos(nanosTimeout); else return false; } // ... } Design note: This method requires a nanosecond argument so as to avoid truncation errors in reporting remaining times. Such precision loss would make it difficult for programmers to ensure that total waiting times are not systematically shorter than specified when re-waits occur. Implementation Considerations
The current thread is assumed to hold the lock associated with this
Reimplemented in AbstractLock::ConditionObject. |
|
Causes the current thread to wait until it is notified or the specified waiting time elapses. This method is behaviorally equivalent to: waitNanos(unit.toNanos(time)) > 0
Reimplemented in AbstractLock::ConditionObject. |
|
Wakes up one waiting thread.
If any threads are waiting on this condition then one is selected for waking up. That thread must then re-acquire the lock before returning from Reimplemented in AbstractLock::ConditionObject. |
|
Wake up all waiting threads.
If any threads are waiting on this condition then they are all woken up. Each thread must re-acquire the lock before it can return from Reimplemented in AbstractLock::ConditionObject. Referenced by CyclicBarrier::breakBarrier(), CyclicBarrier::nextGeneration(), and CyclicBarrier::reset(). |