|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
public interface Condition
Condition factors out the Object monitor
methods(wait
, notify
and notifyAll
) into distinct objects to
give the effect of having multiple wait-sets per object, by
combining them with the use of arbitrary Lock
implementations.
Where a Lock replaces the use of synchronized methods
and statements, a Condition replaces the use of the Object
monitor methods. Conditions(also known as condition queues or
condition variables) provide a means for one thread to
suspend execution(to "wait") until notified by another
thread that some state condition may now be true. 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, just like Object.wait.
A Condition instance is intrinsically bound to a lock.
To obtain a Condition instance for a particular Lock
instance use its newCondition()
method.
As an example, suppose we have a bounded buffer which supports
put and take methods. If a
take is attempted on an empty buffer, then the thread will block
until an item becomes available; if a put is attempted on a
full buffer, then the thread will block until a space becomes available.
We would like to keep waiting put threads and take
threads in separate wait-sets so that we can use the optimization of
only notifying a single thread at a time when items or spaces become
available in the buffer. This can be achieved using two
Condition
instances.
class BoundedBuffer { final Lock lock = new ReentrantLock(); final Condition notFull = lock.newCondition(); final Condition notEmpty = lock.newCondition(); final Object[] items = new Object[100]; int putptr, takeptr, count; public void put(Object x) throws InterruptedException { lock.lock(); try { while (count == items.length) notFull.await(); items[putptr] = x; if (++putptr == items.length) putptr = 0; ++count; notEmpty.signal(); } finally { lock.unlock(); } } public Object take() throws InterruptedException { lock.lock(); try { while (count == 0) notEmpty.await(); Object x = items[takeptr]; if (++takeptr == items.length) takeptr = 0; --count; notFull.signal(); return x; } finally { lock.unlock(); } } }(The
edu.emory.mathcs.backport.java.util.concurrent.ArrayBlockingQueue
class provides
this functionality, so there is no reason to implement this
sample usage class.)
A Condition implementation can provide behavior and semantics
that is different from that of the Object monitor methods, 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,
and can have their own monitor wait
and
notification
methods invoked.
Acquiring the monitor lock of a Condition instance, or using its
monitor methods, has no specified relationship with acquiring the
Lock
associated with that Condition or the use of its
waiting
and signalling
methods.
It is recommended that to avoid confusion you never use Condition
instances in this way, except perhaps within their own implementation.
Except where noted, passing a null value for any parameter
will result in a NullPointerException
being thrown.
Method Summary | |
---|---|
void |
await()
Causes the current thread to wait until it is signalled or interrupted . |
boolean |
await(long time,
TimeUnit unit)
Causes the current thread to wait until it is signalled or interrupted, or the specified waiting time elapses. |
void |
awaitUninterruptibly()
Causes the current thread to wait until it is signalled. |
boolean |
awaitUntil(Date deadline)
Causes the current thread to wait until it is signalled or interrupted, or the specified deadline elapses. |
void |
signal()
Wakes up one waiting thread. |
void |
signalAll()
Wakes up all waiting threads. |
Method Detail |
---|
void await() throws InterruptedException
interrupted
.
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:
signal()
method for this
Condition and the current thread happens to be chosen as the
thread to be awakened; or
signalAll()
method for this
Condition; or
interrupts
the current
thread, and interruption of thread suspension is supported; or
interrupted
while waiting
and interruption of thread suspension is supported,
InterruptedException
is thrown and the current thread's
interrupted status is cleared. It is not specified, in the first
case, whether or not the test for interruption occurs before the lock
is released.
Implementation Considerations
The current thread is assumed to hold the lock associated with this
Condition when this method is called.
It is up to the implementation to determine if this is
the case and if not, how to respond. Typically, an exception will be
thrown(such as IllegalMonitorStateException
) and the
implementation must document that fact.
An implementation can favor responding to an interrupt over normal
method return in response to a signal. In that case the implementation
must ensure that the signal is redirected to another waiting thread, if
there is one.
InterruptedException
- if the current thread is interrupted(and
interruption of thread suspension is supported).void awaitUninterruptibly()
signal()
method for this
Condition and the current thread happens to be chosen as the
thread to be awakened; or
signalAll()
method for this
Condition; or
interrupted
while waiting, it will continue to wait until signalled. When it finally
returns from this method its interrupted status will still be set.
Implementation Considerations
The current thread is assumed to hold the lock associated with this
Condition when this method is called.
It is up to the implementation to determine if this is
the case and if not, how to respond. Typically, an exception will be
thrown(such as IllegalMonitorStateException
) and the
implementation must document that fact.
boolean await(long time, TimeUnit unit) throws InterruptedException
awaitNanos(unit.toNanos(time)) > 0
time
- the maximum time to waitunit
- the time unit of the time argument.
InterruptedException
- if the current thread is interrupted(and
interruption of thread suspension is supported).boolean awaitUntil(Date deadline) throws InterruptedException
signal()
method for this
Condition and the current thread happens to be chosen as the
thread to be awakened; or
signalAll()
method for this
Condition; or
interrupts
the current
thread, and interruption of thread suspension is supported; or
interrupted
while waiting
and interruption of thread suspension is supported,
InterruptedException
is thrown and the current thread's
interrupted status is cleared. It is not specified, in the first
case, whether or not the test for interruption occurs before the lock
is released.
The return value indicates whether the deadline has elapsed,
which can be used as follows:
synchronized boolean aMethod(Date deadline) { boolean stillWaiting = true; while (!conditionBeingWaitedFor) { if (stillWaiting) stillWaiting = theCondition.awaitUntil(deadline); else return false; } // ... }Implementation Considerations The current thread is assumed to hold the lock associated with this Condition when this method is called. It is up to the implementation to determine if this is the case and if not, how to respond. Typically, an exception will be thrown(such as
IllegalMonitorStateException
) and the
implementation must document that fact.
An implementation can favor responding to an interrupt over normal
method return in response to a signal, or over indicating the passing
of the specified deadline. In either case the implementation
must ensure that the signal is redirected to another waiting thread, if
there is one.
deadline
- the absolute time to wait until
InterruptedException
- if the current thread is interrupted(and
interruption of thread suspension is supported).void signal()
void signalAll()
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |