|
Préférences
Moteurs de recherche
|
||||||||||||||||
JavaTM 2 Platform Std. Ed. v1.6.0
java.util.concurrent.locks
|
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. |
long |
awaitNanos(long nanosTimeout)
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
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
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.
If the current thread:
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()
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 three 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
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.
If the current thread's interrupted status is set when it enters this method, or it is 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.
long awaitNanos(long nanosTimeout) throws InterruptedException
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 five 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
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.
If the current thread:
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 method returns an estimate of the number of nanoseconds
remaining to wait given the supplied nanosTimeout
value upon return, or a value less than or equal to zero if it
timed out. This value can be used to determine whether and how
long to re-wait in cases where the wait returns but an awaited
condition still does not hold. Typical uses of this method take
the following form:
synchronized boolean aMethod(long timeout, TimeUnit unit) { long nanosTimeout = unit.toNanos(timeout); while (!conditionBeingWaitedFor) { if (nanosTimeout > 0) nanosTimeout = theCondition.awaitNanos(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
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 elapse of the specified waiting time. In either case the implementation must ensure that the signal is redirected to another waiting thread, if there is one.
nanosTimeout
- the maximum time to wait, in nanoseconds
nanosTimeout
value minus
the time spent waiting upon return from this method.
A positive value may be used as the argument to a
subsequent call to this method to finish waiting out
the desired time. A value less than or equal to zero
indicates that no time remains.
InterruptedException
- if the current thread is interrupted
(and interruption of thread suspension is supported)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
false
if the waiting time detectably elapsed
before return from the method, else true
InterruptedException
- if the current thread is interrupted
(and interruption of thread suspension is supported)boolean awaitUntil(Date deadline) throws InterruptedException
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 five 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
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.
If the current thread:
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
false
if the deadline has elapsed upon return, else
true
InterruptedException
- if the current thread is interrupted
(and interruption of thread suspension is supported)void signal()
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 await
.
void signalAll()
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 await
.