Name

Condition

Synopsis

This interface defines an alternative to the wait( ), notify( ), and notifyAll( ) methods of java.lang.Object. Condition objects are always associated with a corresponding Lock. Obtain a Condition with the newCondition( ) method of Lock.

There are five choices for waiting. The no-argument version of await( ) is the simplest: it blocks until the thread is signaled or interrupted. awaitUninterruptibly( ) blocks until the thread is signaled and ignores interrupts. The other three waiting methods are timed waits: they all wait until signaled, interrupted, or until the specified time elapses. await( ) and awaitUntil( ) return true if they are signaled and false if a timeout occurs. awaitNanos( ) specifies the timeout in nanoseconds. It returns zero or a negative number if the timeout elapses. If it wakes up because of a signal (or because of a spurious wakeup), it returns an estimate of the time remaining in the timeout. If it turns out that the thread needs to continue waiting, this return value can be used as the new timeout value.

The signal( ) and signalAll( ) methods are just like the notify( ) and notifyAll( ) methods of Object. signal( ) wakes up one waiting thread, and signalAll( ) wakes up all waiting threads.

Locking considerations apply to the use of a Condition object just as they apply to the use of the wait( ) and notify( ) methods of Object. Before a thread can call any of the waiting or signaling methods of a Condition, it must hold the Lock associated ...

Get Java in a Nutshell, 5th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.