Top Description Constructors Methods
com.google.common.util.concurrent

pack-priv abstract Class ForwardingCondition

extends Object
implements Condition
Class Inheritance
All Implemented Interfaces
java.util.concurrent.locks.Condition
Known Direct Subclasses
com.google.common.util.concurrent.Striped.WeakSafeCondition
Annotations
@J2ktIncompatible
@ElementTypesAreNonnullByDefault
Imports
com.google.common.annotations.J2ktIncompatible, java.util.Date, java.util.concurrent.TimeUnit, java.util.concurrent.locks.Condition

Forwarding wrapper around a Condition.

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
public void
await()

Implements java.util.concurrent.locks.Condition.await.

Causes the current thread to wait until it is signalled or interrupted.
public boolean
await(long
the maximum time to wait
time
,
TimeUnit
the time unit of the time argument
unit
)

Implements java.util.concurrent.locks.Condition.await.

Causes the current thread to wait until it is signalled or interrupted, or the specified waiting time elapses.
public long
awaitNanos(long
the maximum time to wait, in nanoseconds
nanosTimeout
)

Implements java.util.concurrent.locks.Condition.awaitNanos.

Causes the current thread to wait until it is signalled or interrupted, or the specified waiting time elapses.
public void
awaitUninterruptibly()

Implements java.util.concurrent.locks.Condition.awaitUninterruptibly.

Causes the current thread to wait until it is signalled.
public boolean
awaitUntil(Date
the absolute time to wait until
deadline
)

Implements java.util.concurrent.locks.Condition.awaitUntil.

Causes the current thread to wait until it is signalled or interrupted, or the specified deadline elapses.
pack-priv abstract Condition
public void
signal()

Implements java.util.concurrent.locks.Condition.signal.

Wakes up one waiting thread.
public void
signalAll()

Implements java.util.concurrent.locks.Condition.signalAll.

Wakes up all waiting threads.
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

ForwardingConditionback to summary
pack-priv ForwardingCondition()

Method Detail

awaitback to summary
public void await() throws InterruptedException

Implements java.util.concurrent.locks.Condition.await.

Doc from java.util.concurrent.locks.Condition.await.

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:

  • Some other thread invokes the signal method for this Condition and the current thread happens to be chosen as the thread to be awakened; or
  • Some other thread invokes the signalAll method for this Condition; or
  • Some other thread interrupts the current thread, and interruption of thread suspension is supported; or
  • A "spurious wakeup" occurs.

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 and interruption of thread suspension is supported,
then 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.

Annotations
@Override
Exceptions
InterruptedException:
if the current thread is interrupted (and interruption of thread suspension is supported)
awaitback to summary
public boolean await(long time, TimeUnit unit) throws InterruptedException

Implements java.util.concurrent.locks.Condition.await.

Doc from java.util.concurrent.locks.Condition.await.

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
Parameters
time:long

the maximum time to wait

unit:TimeUnit

the time unit of the time argument

Returns:boolean

false if the waiting time detectably elapsed before return from the method, else true

Annotations
@Override
Exceptions
InterruptedException:
if the current thread is interrupted (and interruption of thread suspension is supported)
awaitNanosback to summary
public long awaitNanos(long nanosTimeout) throws InterruptedException

Implements java.util.concurrent.locks.Condition.awaitNanos.

Doc from java.util.concurrent.locks.Condition.awaitNanos.

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:

  • Some other thread invokes the signal method for this Condition and the current thread happens to be chosen as the thread to be awakened; or
  • Some other thread invokes the signalAll method for this Condition; or
  • Some other thread interrupts the current thread, and interruption of thread suspension is supported; or
  • The specified waiting time elapses; or
  • A "spurious wakeup" occurs.

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 and interruption of thread suspension is supported,
then 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:

 boolean aMethod(long timeout, TimeUnit unit)
    throws InterruptedException {
  long nanosRemaining = unit.toNanos(timeout);
  lock.lock();
  try {
    while (!conditionBeingWaitedFor()) {
      if (nanosRemaining <= 0L)
        return false;
      nanosRemaining = theCondition.awaitNanos(nanosRemaining);
    }
    // ...
    return true;
  } finally {
    lock.unlock();
  }
}

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.

Parameters
nanosTimeout:long

the maximum time to wait, in nanoseconds

Returns:long

an estimate of the 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.

Annotations
@Override
Exceptions
InterruptedException:
if the current thread is interrupted (and interruption of thread suspension is supported)
awaitUninterruptiblyback to summary
public void awaitUninterruptibly()

Implements java.util.concurrent.locks.Condition.awaitUninterruptibly.

Doc from java.util.concurrent.locks.Condition.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:

  • Some other thread invokes the signal method for this Condition and the current thread happens to be chosen as the thread to be awakened; or
  • Some other thread invokes the signalAll method for this Condition; or
  • A "spurious wakeup" occurs.

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.

Annotations
@Override
awaitUntilback to summary
public boolean awaitUntil(Date deadline) throws InterruptedException

Implements java.util.concurrent.locks.Condition.awaitUntil.

Doc from java.util.concurrent.locks.Condition.awaitUntil.

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:

  • Some other thread invokes the signal method for this Condition and the current thread happens to be chosen as the thread to be awakened; or
  • Some other thread invokes the signalAll method for this Condition; or
  • Some other thread interrupts the current thread, and interruption of thread suspension is supported; or
  • The specified deadline elapses; or
  • A "spurious wakeup" occurs.

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 and interruption of thread suspension is supported,
then 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:

 boolean aMethod(Date deadline)
    throws InterruptedException {
  boolean stillWaiting = true;
  lock.lock();
  try {
    while (!conditionBeingWaitedFor()) {
      if (!stillWaiting)
        return false;
      stillWaiting = theCondition.awaitUntil(deadline);
    }
    // ...
    return true;
  } finally {
    lock.unlock();
  }
}

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.

Parameters
deadline:Date

the absolute time to wait until

Returns:boolean

false if the deadline has elapsed upon return, else true

Annotations
@Override
Exceptions
InterruptedException:
if the current thread is interrupted (and interruption of thread suspension is supported)
delegateback to summary
pack-priv abstract Condition delegate()
signalback to summary
public void signal()

Implements java.util.concurrent.locks.Condition.signal.

Doc from java.util.concurrent.locks.Condition.signal.

Wakes up one waiting thread.

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.

Implementation Considerations

An implementation may (and typically does) require that the current thread hold the lock associated with this Condition when this method is called. Implementations must document this precondition and any actions taken if the lock is not held. Typically, an exception such as IllegalMonitorStateException will be thrown.

Annotations
@Override
signalAllback to summary
public void signalAll()

Implements java.util.concurrent.locks.Condition.signalAll.

Doc from java.util.concurrent.locks.Condition.signalAll.

Wakes 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 can return from await.

Implementation Considerations

An implementation may (and typically does) require that the current thread hold the lock associated with this Condition when this method is called. Implementations must document this precondition and any actions taken if the lock is not held. Typically, an exception such as IllegalMonitorStateException will be thrown.

Annotations
@Override