java.util.concurrent.locks
Class ReentrantReadWriteLock.WriterConditionObject

java.lang.Object
  extended by java.util.concurrent.locks.ReentrantReadWriteLock.WriterConditionObject
All Implemented Interfaces:
Condition, Serializable
Enclosing class:
ReentrantReadWriteLock

public static class ReentrantReadWriteLock.WriterConditionObject
extends Object
implements Condition, Serializable

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.

See Also:
Serialized Form

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

ReentrantReadWriteLock.WriterConditionObject

protected ReentrantReadWriteLock.WriterConditionObject(ReentrantReadWriteLock lock)
Constructor for use by subclasses to create a WriterConditionObject associated with given lock.

Parameters:
lock - the lock for this condition
Throws:
NullPointerException - if lock null
Method Detail

signal

public void signal()
Wakes up one waiting thread.

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.

Specified by:
signal in interface Condition
Throws:
IllegalMonitorStateException - if the lock associated with this Condition is not held by the current thread

signalAll

public void signalAll()
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 returns.

Specified by:
signalAll in interface Condition
Throws:
IllegalMonitorStateException - if the lock associated with this Condition is not held by the current thread

awaitUninterruptibly

public void awaitUninterruptibly()
Causes the current thread to wait until it is signalled.

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:

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.

Specified by:
awaitUninterruptibly in interface Condition
Throws:
IllegalMonitorStateException - if the lock associated with this Condition is not held by the current thread

await

public void await()
           throws InterruptedException
Causes the current thread to wait until it is signalled or 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:

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.

Specified by:
await in interface Condition
Throws:
InterruptedException - if the current thread is interrupted
IllegalMonitorStateException - if the lock associated with this Condition is not held by the current thread

awaitNanos

public long awaitNanos(long nanosTimeout)
                throws InterruptedException
Causes the current thread to wait until it is signalled or interrupted, 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 five 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.

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.

Specified by:
awaitNanos in interface Condition
Parameters:
nanosTimeout - the maximum time to wait, in nanoseconds
Returns:
A value less than or equal to zero if the wait has timed out; otherwise an estimate, that is strictly less than the nanosTimeout argument, of the time still remaining when this method returned.
Throws:
InterruptedException - if the current thread is interrupted.
IllegalMonitorStateException - if the lock associated with this Condition is not held by the current thread

awaitUntil

public boolean awaitUntil(Date deadline)
                   throws InterruptedException
Causes the current thread to wait until it is signalled or interrupted, or the specified deadline 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 five 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.

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.

Specified by:
awaitUntil in interface Condition
Parameters:
deadline - the absolute time to wait until
Returns:
false if the deadline has elapsed upon return, else true.
Throws:
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 null

await

public boolean await(long time,
                     TimeUnit unit)
              throws InterruptedException
Causes the current thread to wait until it is signalled or interrupted, or the specified waiting time elapses. This method is behaviorally equivalent to:
   awaitNanos(unit.toNanos(time)) > 0
 

Specified by:
await in interface Condition
Parameters:
time - the maximum time to wait
unit - the time unit of the time argument.
Returns:
false if the waiting time detectably elapsed before return from the method, else true.
Throws:
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 null

hasWaiters

public boolean hasWaiters()
Queries whether any threads are waiting on this condition. Note that because timeouts and interrupts may occur at any time, a true return does not guarantee that a future signal will awaken any threads. This method is designed primarily for use in monitoring of the system state.

Returns:
true if there are any waiting threads.
Throws:
IllegalMonitorStateException - if the lock associated with this Condition is not held by the current thread

getWaitQueueLength

public int getWaitQueueLength()
Returns an estimate of the number of threads waiting on this condition. Note that because timeouts and interrupts may occur at any time, the estimate serves only as an upper bound on the actual number of waiters. This method is designed for use in monitoring of the system state, not for synchronization control.

Returns:
the estimated number of waiting threads.
Throws:
IllegalMonitorStateException - if the lock associated with this Condition is not held by the current thread

getWaitingThreads

protected Collection<Thread> getWaitingThreads()
Returns a collection containing those threads that may be waiting on this Condition. Because the actual set of threads may change dynamically while constructing this result, the returned collection is only a best-effort estimate. The elements of the returned collection are in no particular order. This method is designed to facilitate construction of subclasses that provide more extensive condition monitoring facilities.

Returns:
the collection of threads
Throws:
IllegalMonitorStateException - if the lock associated with this Condition is not held by the current thread