java.lang.Objectjava.util.concurrent.locks.ReentrantReadWriteLock.WriterConditionObject
Condition implementation for use with ReentrantReadWriteLock.
This class supports the same basic semantics and styles of
usage as the Object
monitor methods. Methods may be
invoked only when holding the write lock of the
ReentrantReadWriteLock associated with this
Condition. Failure to comply results in IllegalMonitorStateException
.
In addition to implementing the Condition
interface,
this class defines methods hasWaiters and
getWaitQueueLength, as well as some associated
protected access methods, that may be useful for
instrumentation and monitoring.
Constructor Summary | |
---|---|
protected |
ReentrantReadWriteLock.WriterConditionObject(ReentrantReadWriteLock lock)
Constructor for use by subclasses to create a WriterConditionObject associated with given lock. |
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. |
protected Collection<Thread> |
getWaitingThreads()
Returns a collection containing those threads that may be waiting on this Condition. |
int |
getWaitQueueLength()
Returns an estimate of the number of threads waiting on this condition. |
boolean |
hasWaiters()
Queries whether any threads are waiting on this condition. |
void |
signal()
Wakes up one waiting thread. |
void |
signalAll()
Wake up all waiting threads. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
protected ReentrantReadWriteLock.WriterConditionObject(ReentrantReadWriteLock lock)
lock
- the lock for this condition
NullPointerException
- if lock nullMethod Detail |
---|
public void signal()
If any threads are waiting on this condition then one is selected for waking up. This implementation always chooses to wake up the longest-waiting thread whose wait has not been interrupted or timed out. That thread must then re-acquire the lock before it returns. The order in which it will do so is the same as that for threads initially acquiring the lock, which is in the default case not specified, but for fair locks favors those threads that have been waiting the longest. Note that an awakened thread can return, at the soonest, only after the current thread releases the lock associated with this Condition.
signal
in interface Condition
IllegalMonitorStateException
- if the lock associated
with this Condition is not held by the current threadpublic 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 returns.
signalAll
in interface Condition
IllegalMonitorStateException
- if the lock associated
with this Condition is not held by the current threadpublic 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
has been waiting the longest of all waiting threads; 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 interrupt 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.
awaitUninterruptibly
in interface Condition
IllegalMonitorStateException
- if the lock associated
with this Condition is not held by the current threadpublic 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
has been waiting the longest of all waiting threads; or
signalAll()
method
for this Condition; or
interrupts
the current thread; 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 has its interrupted status set on
entry to this method or is interrupted
while waiting, then InterruptedException
is thrown and the current thread's
interrupted status is cleared. This implementation favors
responding to an interrupt over normal method return in
response to a signal.
await
in interface Condition
InterruptedException
- if the current thread is
interrupted
IllegalMonitorStateException
- if the lock associated
with this Condition is not held by the current threadpublic 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
has been waiting the longest of all waiting threads; or
signalAll()
method
for this Condition; or
interrupts
the current thread; 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 has its interrupted status set on
entry to this method or is interrupted
while waiting, then InterruptedException
is thrown and the current thread's
interrupted status is cleared. This implementation favors
responding to an interrupt over normal method return in
response to a signal or timeout.
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.
awaitNanos
in interface Condition
nanosTimeout
- the maximum time to wait, in nanoseconds
InterruptedException
- if the current thread is
interrupted.
IllegalMonitorStateException
- if the lock associated
with this Condition is not held by the current threadpublic 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
has been waiting the longest of all waiting threads; or
signalAll()
method
for this Condition; or
interrupts
the current thread; 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 has its interrupted status set on
entry to this method or is interrupted
while waiting, then InterruptedException
is thrown and the current thread's
interrupted status is cleared. This implementation favors
responding to an interrupt over normal method return in
response to a signal or timeout.
awaitUntil
in interface Condition
deadline
- the absolute time to wait until
InterruptedException
- if the current thread is interrupted
IllegalMonitorStateException
- if the lock associated
with this Condition is not held by the current thread
NullPointerException
- if deadline is nullpublic boolean await(long time, TimeUnit unit) throws InterruptedException
awaitNanos(unit.toNanos(time)) > 0
await
in interface Condition
time
- the maximum time to waitunit
- the time unit of the time argument.
InterruptedException
- if the current thread is
interrupted
IllegalMonitorStateException
- if the lock associated
with this Condition is not held by the current thread
NullPointerException
- if unit is nullpublic boolean hasWaiters()
IllegalMonitorStateException
- if the lock associated
with this Condition is not held by the current threadpublic int getWaitQueueLength()
IllegalMonitorStateException
- if the lock associated
with this Condition is not held by the current threadprotected Collection<Thread> getWaitingThreads()
IllegalMonitorStateException
- if the lock associated
with this Condition is not held by the current thread