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

public final Class Monitor

extends Object
Class Inheritance
Annotations
@J2ktIncompatible
@GwtIncompatible
@SuppressWarnings:GuardedBy
@ElementTypesAreNonnullByDefault
Static Imports
com.google.common.base.Preconditions.checkNotNull, com.google.common.util.concurrent.Internal.toNanosSaturated

A synchronization abstraction supporting waiting on arbitrary boolean conditions.

This class is intended as a replacement for ReentrantLock. Code using Monitor is less error-prone and more readable than code using ReentrantLock, without significant performance loss. Monitor even has the potential for performance gain by optimizing the evaluation and signaling of conditions. Signaling is entirely implicit. By eliminating explicit signaling, this class can guarantee that only one thread is awakened when a condition becomes true (no "signaling storms" due to use of Condition.signalAll) and that no signals are lost (no "hangs" due to incorrect use of Condition.signal).

A thread is said to occupy a monitor if it has entered the monitor but not yet left. Only one thread may occupy a given monitor at any moment. A monitor is also reentrant, so a thread may enter a monitor any number of times, and then must leave the same number of times. The enter and leave operations have the same synchronization semantics as the built-in Java language synchronization primitives.

A call to any of the enter methods with void return type should always be followed immediately by a try/finally block to ensure that the current thread leaves the monitor cleanly:

monitor.enter();
try {
  // do things while occupying the monitor
} finally {
  monitor.leave();
}

A call to any of the enter methods with boolean return type should always appear as the condition of an if statement containing a try/finally block to ensure that the current thread leaves the monitor cleanly:

if (monitor.tryEnter()) {
  try {
    // do things while occupying the monitor
  } finally {
    monitor.leave();
  }
} else {
  // do other things since the monitor was not available
}

Comparison with synchronized and ReentrantLock

The following examples show a simple threadsafe holder expressed using synchronized, ReentrantLock, and Monitor.

synchronized

This version is the fewest lines of code, largely because the synchronization mechanism used is built into the language and runtime. But the programmer has to remember to avoid a couple of common bugs: The wait() must be inside a while instead of an if, and notifyAll() must be used instead of notify() because there are two different logical conditions being awaited.

public class SafeBox<V> {
  private V value;

  public synchronized V get() throws InterruptedException {
    while (value == null) {
      wait();
    }
    V result = value;
    value = null;
    notifyAll();
    return result;
  }

  public synchronized void set(V newValue) throws InterruptedException {
    while (value != null) {
      wait();
    }
    value = newValue;
    notifyAll();
  }
}

ReentrantLock

This version is much more verbose than the synchronized version, and still suffers from the need for the programmer to remember to use while instead of if. However, one advantage is that we can introduce two separate Condition objects, which allows us to use signal() instead of signalAll(), which may be a performance benefit.

public class SafeBox<V> {
  private V value;
  private final ReentrantLock lock = new ReentrantLock();
  private final Condition valuePresent = lock.newCondition();
  private final Condition valueAbsent = lock.newCondition();

  public V get() throws InterruptedException {
    lock.lock();
    try {
      while (value == null) {
        valuePresent.await();
      }
      V result = value;
      value = null;
      valueAbsent.signal();
      return result;
    } finally {
      lock.unlock();
    }
  }

  public void set(V newValue) throws InterruptedException {
    lock.lock();
    try {
      while (value != null) {
        valueAbsent.await();
      }
      value = newValue;
      valuePresent.signal();
    } finally {
      lock.unlock();
    }
  }
}

Monitor

This version adds some verbosity around the Guard objects, but removes that same verbosity, and more, from the get and set methods. Monitor implements the same efficient signaling as we had to hand-code in the ReentrantLock version above. Finally, the programmer no longer has to hand-code the wait loop, and therefore doesn't have to remember to use while instead of if.

public class SafeBox<V> {
  private V value;
  private final Monitor monitor = new Monitor();
  private final Monitor.Guard valuePresent = monitor.newGuard(() -> value != null);
  private final Monitor.Guard valueAbsent = monitor.newGuard(() -> value == null);

  public V get() throws InterruptedException {
    monitor.enterWhen(valuePresent);
    try {
      V result = value;
      value = null;
      return result;
    } finally {
      monitor.leave();
    }
  }

  public void set(V newValue) throws InterruptedException {
    monitor.enterWhen(valueAbsent);
    try {
      value = newValue;
    } finally {
      monitor.leave();
    }
  }
}
Authors
Justin T. Sampson, Martin Buchholz
Since
10.0

Nested and Inner Type Summary

Modifier and TypeClass and Description
public abstract static class
Monitor.Guard

A boolean condition for which a thread may wait.

Field Summary

Modifier and TypeField and Description
private Monitor.Guard
activeGuards

The guards associated with this monitor that currently have waiters (waiterCount > 0).

private final boolean
fair

Whether this monitor is fair.

private final ReentrantLock
lock

The lock underlying this monitor.

Constructor Summary

AccessConstructor and Description
public
Monitor()

Creates a monitor with a non-fair (but fast) ordering policy.

public
Monitor(boolean
whether this monitor should use a fair ordering policy rather than a non-fair (but fast) one
fair
)

Creates a monitor with the given ordering policy.

Method Summary

Modifier and TypeMethod and Description
private void
await(Monitor.Guard guard, boolean signalBeforeWaiting)

private boolean
awaitNanos(Monitor.Guard guard, long nanos, boolean signalBeforeWaiting)

Caller should check before calling that guard is not satisfied.

private void
awaitUninterruptibly(Monitor.Guard guard, boolean signalBeforeWaiting)

private void
beginWaitingFor(Monitor.Guard guard)

Records that the current thread is about to wait on the specified guard.

private void
endWaitingFor(Monitor.Guard guard)

Records that the current thread is no longer waiting on the specified guard.

public void
enter()

Enters this monitor.

public boolean

Returns:

whether the monitor was entered
enter
(Duration time)

Enters this monitor.

public boolean

Returns:

whether the monitor was entered
enter
(long time, TimeUnit unit)

Enters this monitor.

public boolean

Returns:

whether the monitor was entered, which guarantees that the guard is now satisfied
enterIf
(Monitor.Guard guard)

Enters this monitor if the guard is satisfied.

public boolean

Returns:

whether the monitor was entered, which guarantees that the guard is now satisfied
enterIf
(Monitor.Guard guard, Duration time)

Enters this monitor if the guard is satisfied.

public boolean

Returns:

whether the monitor was entered, which guarantees that the guard is now satisfied
enterIf
(Monitor.Guard guard, long time, TimeUnit unit)

Enters this monitor if the guard is satisfied.

public boolean

Returns:

whether the monitor was entered, which guarantees that the guard is now satisfied
enterIfInterruptibly
(Monitor.Guard guard)

Enters this monitor if the guard is satisfied.

public boolean

Returns:

whether the monitor was entered, which guarantees that the guard is now satisfied
enterIfInterruptibly
(Monitor.Guard guard, Duration time)

Enters this monitor if the guard is satisfied.

public boolean

Returns:

whether the monitor was entered, which guarantees that the guard is now satisfied
enterIfInterruptibly
(Monitor.Guard guard, long time, TimeUnit unit)

Enters this monitor if the guard is satisfied.

public void
enterInterruptibly()

Enters this monitor.

public boolean

Returns:

whether the monitor was entered
enterInterruptibly
(Duration time)

Enters this monitor.

public boolean

Returns:

whether the monitor was entered
enterInterruptibly
(long time, TimeUnit unit)

Enters this monitor.

public void
enterWhen(Monitor.Guard guard)

Enters this monitor when the guard is satisfied.

public boolean

Returns:

whether the monitor was entered, which guarantees that the guard is now satisfied
enterWhen
(Monitor.Guard guard, Duration time)

Enters this monitor when the guard is satisfied.

public boolean

Returns:

whether the monitor was entered, which guarantees that the guard is now satisfied
enterWhen
(Monitor.Guard guard, long time, TimeUnit unit)

Enters this monitor when the guard is satisfied.

public void
enterWhenUninterruptibly(Monitor.Guard guard)

Enters this monitor when the guard is satisfied.

public boolean

Returns:

whether the monitor was entered, which guarantees that the guard is now satisfied
enterWhenUninterruptibly
(Monitor.Guard guard, Duration time)

Enters this monitor when the guard is satisfied.

public boolean

Returns:

whether the monitor was entered, which guarantees that the guard is now satisfied
enterWhenUninterruptibly
(Monitor.Guard guard, long time, TimeUnit unit)

Enters this monitor when the guard is satisfied.

public int
getOccupiedDepth()

Returns the number of times the current thread has entered this monitor in excess of the number of times it has left.

public int
getQueueLength()

Returns an estimate of the number of threads waiting to enter this monitor.

public int
getWaitQueueLength(Monitor.Guard guard)

Returns an estimate of the number of threads waiting for the given guard to become satisfied.

public boolean
hasQueuedThread(Thread thread)

Queries whether the given thread is waiting to enter this monitor.

public boolean
hasQueuedThreads()

Returns whether any threads are waiting to enter this monitor.

public boolean
hasWaiters(Monitor.Guard guard)

Queries whether any threads are waiting for the given guard to become satisfied.

private static long
initNanoTime(long timeoutNanos)

Returns System.nanoTime() unless the timeout has already elapsed.

public boolean
isFair()

Returns whether this monitor is using a fair ordering policy.

public boolean
isOccupied()

Returns whether this monitor is occupied by any thread.

public boolean
isOccupiedByCurrentThread()

Returns whether the current thread is occupying this monitor (has entered more times than it has left).

private boolean
isSatisfied(Monitor.Guard guard)

Exactly like guard.isSatisfied(), but in addition signals all waiting threads in the (hopefully unlikely) event that isSatisfied() throws.

public void
leave()

Leaves this monitor.

public Monitor.Guard
newGuard(final BooleanSupplier
the new guard's boolean condition (see isSatisfied())
isSatisfied
)

Creates a new guard for this monitor.

private static long
remainingNanos(long startTime, long timeoutNanos)

Returns the remaining nanos until the given timeout, or 0L if the timeout has already elapsed.

private void
signalAllWaiters()

Signals all threads waiting on guards.

private void
signalNextWaiter()

Signals some other thread waiting on a satisfied guard, if one exists.

private static long
toSafeNanos(long time, TimeUnit unit)

Returns unit.toNanos(time), additionally ensuring the returned value is not at risk of overflowing or underflowing, by bounding the value between 0 and (Long.MAX_VALUE / 4) * 3.

public boolean

Returns:

whether the monitor was entered
tryEnter
()

Enters this monitor if it is possible to do so immediately.

public boolean

Returns:

whether the monitor was entered, which guarantees that the guard is now satisfied
tryEnterIf
(Monitor.Guard guard)

Enters this monitor if it is possible to do so immediately and the guard is satisfied.

public void
waitFor(Monitor.Guard guard)

Waits for the guard to be satisfied.

public boolean

Returns:

whether the guard is now satisfied
waitFor
(Monitor.Guard guard, Duration time)

Waits for the guard to be satisfied.

public boolean

Returns:

whether the guard is now satisfied
waitFor
(Monitor.Guard guard, long time, TimeUnit unit)

Waits for the guard to be satisfied.

public void
waitForUninterruptibly(Monitor.Guard guard)

Waits for the guard to be satisfied.

public boolean

Returns:

whether the guard is now satisfied
waitForUninterruptibly
(Monitor.Guard guard, Duration time)

Waits for the guard to be satisfied.

public boolean

Returns:

whether the guard is now satisfied
waitForUninterruptibly
(Monitor.Guard guard, long time, TimeUnit unit)

Waits for the guard to be satisfied.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

activeGuardsback to summary
private Monitor.Guard activeGuards

The guards associated with this monitor that currently have waiters (waiterCount > 0). A linked list threaded through the Guard.next field.

Annotations
@GuardedBy:lock
@CheckForNull
fairback to summary
private final boolean fair

Whether this monitor is fair.

lockback to summary
private final ReentrantLock lock

The lock underlying this monitor.

Constructor Detail

Monitorback to summary
public Monitor()

Creates a monitor with a non-fair (but fast) ordering policy. Equivalent to Monitor(false).

Monitorback to summary
public Monitor(boolean fair)

Creates a monitor with the given ordering policy.

Parameters
fair:boolean

whether this monitor should use a fair ordering policy rather than a non-fair (but fast) one

Method Detail

awaitback to summary
private void await(Monitor.Guard guard, boolean signalBeforeWaiting) throws InterruptedException
Annotations
@GuardedBy:lock
awaitNanosback to summary
private boolean awaitNanos(Monitor.Guard guard, long nanos, boolean signalBeforeWaiting) throws InterruptedException

Caller should check before calling that guard is not satisfied.

Annotations
@GuardedBy:lock
awaitUninterruptiblyback to summary
private void awaitUninterruptibly(Monitor.Guard guard, boolean signalBeforeWaiting)
Annotations
@GuardedBy:lock
beginWaitingForback to summary
private void beginWaitingFor(Monitor.Guard guard)

Records that the current thread is about to wait on the specified guard.

Annotations
@GuardedBy:lock
endWaitingForback to summary
private void endWaitingFor(Monitor.Guard guard)

Records that the current thread is no longer waiting on the specified guard.

Annotations
@GuardedBy:lock
enterback to summary
public void enter()

Enters this monitor. Blocks indefinitely.

enterback to summary
public boolean enter(Duration time)

Enters this monitor. Blocks at most the given time.

Returns:boolean

whether the monitor was entered

Since
28.0
enterback to summary
public boolean enter(long time, TimeUnit unit)

Enters this monitor. Blocks at most the given time.

Returns:boolean

whether the monitor was entered

Annotations
@SuppressWarnings:GoodTime
enterIfback to summary
public boolean enterIf(Monitor.Guard guard)

Enters this monitor if the guard is satisfied. Blocks indefinitely acquiring the lock, but does not wait for the guard to be satisfied.

Returns:boolean

whether the monitor was entered, which guarantees that the guard is now satisfied

enterIfback to summary
public boolean enterIf(Monitor.Guard guard, Duration time)

Enters this monitor if the guard is satisfied. Blocks at most the given time acquiring the lock, but does not wait for the guard to be satisfied.

Returns:boolean

whether the monitor was entered, which guarantees that the guard is now satisfied

Since
28.0
enterIfback to summary
public boolean enterIf(Monitor.Guard guard, long time, TimeUnit unit)

Enters this monitor if the guard is satisfied. Blocks at most the given time acquiring the lock, but does not wait for the guard to be satisfied.

Returns:boolean

whether the monitor was entered, which guarantees that the guard is now satisfied

Annotations
@SuppressWarnings:GoodTime
enterIfInterruptiblyback to summary
public boolean enterIfInterruptibly(Monitor.Guard guard) throws InterruptedException

Enters this monitor if the guard is satisfied. Blocks indefinitely acquiring the lock, but does not wait for the guard to be satisfied, and may be interrupted.

Returns:boolean

whether the monitor was entered, which guarantees that the guard is now satisfied

Exceptions
InterruptedException:
if interrupted while waiting
enterIfInterruptiblyback to summary
public boolean enterIfInterruptibly(Monitor.Guard guard, Duration time) throws InterruptedException

Enters this monitor if the guard is satisfied. Blocks at most the given time acquiring the lock, but does not wait for the guard to be satisfied, and may be interrupted.

Returns:boolean

whether the monitor was entered, which guarantees that the guard is now satisfied

Since
28.0
enterIfInterruptiblyback to summary
public boolean enterIfInterruptibly(Monitor.Guard guard, long time, TimeUnit unit) throws InterruptedException

Enters this monitor if the guard is satisfied. Blocks at most the given time acquiring the lock, but does not wait for the guard to be satisfied, and may be interrupted.

Returns:boolean

whether the monitor was entered, which guarantees that the guard is now satisfied

Annotations
@SuppressWarnings:GoodTime
enterInterruptiblyback to summary
public void enterInterruptibly() throws InterruptedException

Enters this monitor. Blocks indefinitely, but may be interrupted.

Exceptions
InterruptedException:
if interrupted while waiting
enterInterruptiblyback to summary
public boolean enterInterruptibly(Duration time) throws InterruptedException

Enters this monitor. Blocks at most the given time, and may be interrupted.

Returns:boolean

whether the monitor was entered

Exceptions
InterruptedException:
if interrupted while waiting
Since
28.0
enterInterruptiblyback to summary
public boolean enterInterruptibly(long time, TimeUnit unit) throws InterruptedException

Enters this monitor. Blocks at most the given time, and may be interrupted.

Returns:boolean

whether the monitor was entered

Annotations
@SuppressWarnings:GoodTime
Exceptions
InterruptedException:
if interrupted while waiting
enterWhenback to summary
public void enterWhen(Monitor.Guard guard) throws InterruptedException

Enters this monitor when the guard is satisfied. Blocks indefinitely, but may be interrupted.

Exceptions
InterruptedException:
if interrupted while waiting
enterWhenback to summary
public boolean enterWhen(Monitor.Guard guard, Duration time) throws InterruptedException

Enters this monitor when the guard is satisfied. Blocks at most the given time, including both the time to acquire the lock and the time to wait for the guard to be satisfied, and may be interrupted.

Returns:boolean

whether the monitor was entered, which guarantees that the guard is now satisfied

Exceptions
InterruptedException:
if interrupted while waiting
Since
28.0
enterWhenback to summary
public boolean enterWhen(Monitor.Guard guard, long time, TimeUnit unit) throws InterruptedException

Enters this monitor when the guard is satisfied. Blocks at most the given time, including both the time to acquire the lock and the time to wait for the guard to be satisfied, and may be interrupted.

Returns:boolean

whether the monitor was entered, which guarantees that the guard is now satisfied

Annotations
@SuppressWarnings:GoodTime
Exceptions
InterruptedException:
if interrupted while waiting
enterWhenUninterruptiblyback to summary
public void enterWhenUninterruptibly(Monitor.Guard guard)

Enters this monitor when the guard is satisfied. Blocks indefinitely.

enterWhenUninterruptiblyback to summary
public boolean enterWhenUninterruptibly(Monitor.Guard guard, Duration time)

Enters this monitor when the guard is satisfied. Blocks at most the given time, including both the time to acquire the lock and the time to wait for the guard to be satisfied.

Returns:boolean

whether the monitor was entered, which guarantees that the guard is now satisfied

Since
28.0
enterWhenUninterruptiblyback to summary
public boolean enterWhenUninterruptibly(Monitor.Guard guard, long time, TimeUnit unit)

Enters this monitor when the guard is satisfied. Blocks at most the given time, including both the time to acquire the lock and the time to wait for the guard to be satisfied.

Returns:boolean

whether the monitor was entered, which guarantees that the guard is now satisfied

Annotations
@SuppressWarnings:GoodTime
getOccupiedDepthback to summary
public int getOccupiedDepth()

Returns the number of times the current thread has entered this monitor in excess of the number of times it has left. Returns 0 if the current thread is not occupying this monitor.

getQueueLengthback to summary
public int getQueueLength()

Returns an estimate of the number of threads waiting to enter this monitor. The value is only an estimate because the number of threads may change dynamically while this method traverses internal data structures. This method is designed for use in monitoring of the system state, not for synchronization control.

getWaitQueueLengthback to summary
public int getWaitQueueLength(Monitor.Guard guard)

Returns an estimate of the number of threads waiting for the given guard to become satisfied. 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.

hasQueuedThreadback to summary
public boolean hasQueuedThread(Thread thread)

Queries whether the given thread is waiting to enter this monitor. Note that because cancellations may occur at any time, a true return does not guarantee that this thread will ever enter this monitor. This method is designed primarily for use in monitoring of the system state.

hasQueuedThreadsback to summary
public boolean hasQueuedThreads()

Returns whether any threads are waiting to enter this monitor. Note that because cancellations may occur at any time, a true return does not guarantee that any other thread will ever enter this monitor. This method is designed primarily for use in monitoring of the system state.

hasWaitersback to summary
public boolean hasWaiters(Monitor.Guard guard)

Queries whether any threads are waiting for the given guard to become satisfied. Note that because timeouts and interrupts may occur at any time, a true return does not guarantee that the guard becoming satisfied in the future will awaken any threads. This method is designed primarily for use in monitoring of the system state.

initNanoTimeback to summary
private static long initNanoTime(long timeoutNanos)

Returns System.nanoTime() unless the timeout has already elapsed. Returns 0L if and only if the timeout has already elapsed.

isFairback to summary
public boolean isFair()

Returns whether this monitor is using a fair ordering policy.

isOccupiedback to summary
public boolean isOccupied()

Returns whether this monitor is occupied by any thread. This method is designed for use in monitoring of the system state, not for synchronization control.

isOccupiedByCurrentThreadback to summary
public boolean isOccupiedByCurrentThread()

Returns whether the current thread is occupying this monitor (has entered more times than it has left).

isSatisfiedback to summary
private boolean isSatisfied(Monitor.Guard guard)

Exactly like guard.isSatisfied(), but in addition signals all waiting threads in the (hopefully unlikely) event that isSatisfied() throws.

Annotations
@GuardedBy:lock
leaveback to summary
public void leave()

Leaves this monitor. May be called only by a thread currently occupying this monitor.

newGuardback to summary
public Monitor.Guard newGuard(final BooleanSupplier isSatisfied)

Creates a new guard for this monitor.

Parameters
isSatisfied:BooleanSupplier

the new guard's boolean condition (see isSatisfied())

Since
21.0
remainingNanosback to summary
private static long remainingNanos(long startTime, long timeoutNanos)

Returns the remaining nanos until the given timeout, or 0L if the timeout has already elapsed. Caller must have previously sanitized timeoutNanos using toSafeNanos.

signalAllWaitersback to summary
private void signalAllWaiters()

Signals all threads waiting on guards.

Annotations
@GuardedBy:lock
signalNextWaiterback to summary
private void signalNextWaiter()

Signals some other thread waiting on a satisfied guard, if one exists.

We manage calls to this method carefully, to signal only when necessary, but never losing a signal, which is the classic problem of this kind of concurrency construct. We must signal if the current thread is about to relinquish the lock and may have changed the state protected by the monitor, thereby causing some guard to be satisfied.

In addition, any thread that has been signalled when its guard was satisfied acquires the responsibility of signalling the next thread when it again relinquishes the lock. Unlike a normal Condition, there is no guarantee that an interrupted thread has not been signalled, since the concurrency control must manage multiple Conditions. So this method must generally be called when waits are interrupted.

On the other hand, if a signalled thread wakes up to discover that its guard is still not satisfied, it does *not* need to call this method before returning to wait. This can only happen due to spurious wakeup (ignorable) or another thread acquiring the lock before the current thread can and returning the guard to the unsatisfied state. In the latter case the other thread (last thread modifying the state protected by the monitor) takes over the responsibility of signalling the next waiter.

This method must not be called from within a beginWaitingFor/endWaitingFor block, or else the current thread's guard might be mistakenly signalled, leading to a lost signal.

Annotations
@GuardedBy:lock
toSafeNanosback to summary
private static long toSafeNanos(long time, TimeUnit unit)

Returns unit.toNanos(time), additionally ensuring the returned value is not at risk of overflowing or underflowing, by bounding the value between 0 and (Long.MAX_VALUE / 4) * 3. Actually waiting for more than 219 years is not supported!

tryEnterback to summary
public boolean tryEnter()

Enters this monitor if it is possible to do so immediately. Does not block.

Note

This method disregards the fairness setting of this monitor.

Returns:boolean

whether the monitor was entered

tryEnterIfback to summary
public boolean tryEnterIf(Monitor.Guard guard)

Enters this monitor if it is possible to do so immediately and the guard is satisfied. Does not block acquiring the lock and does not wait for the guard to be satisfied.

Note

This method disregards the fairness setting of this monitor.

Returns:boolean

whether the monitor was entered, which guarantees that the guard is now satisfied

waitForback to summary
public void waitFor(Monitor.Guard guard) throws InterruptedException

Waits for the guard to be satisfied. Waits indefinitely, but may be interrupted. May be called only by a thread currently occupying this monitor.

Exceptions
InterruptedException:
if interrupted while waiting
waitForback to summary
public boolean waitFor(Monitor.Guard guard, Duration time) throws InterruptedException

Waits for the guard to be satisfied. Waits at most the given time, and may be interrupted. May be called only by a thread currently occupying this monitor.

Returns:boolean

whether the guard is now satisfied

Exceptions
InterruptedException:
if interrupted while waiting
Since
28.0
waitForback to summary
public boolean waitFor(Monitor.Guard guard, long time, TimeUnit unit) throws InterruptedException

Waits for the guard to be satisfied. Waits at most the given time, and may be interrupted. May be called only by a thread currently occupying this monitor.

Returns:boolean

whether the guard is now satisfied

Annotations
@SuppressWarnings:GoodTime
Exceptions
InterruptedException:
if interrupted while waiting
waitForUninterruptiblyback to summary
public void waitForUninterruptibly(Monitor.Guard guard)

Waits for the guard to be satisfied. Waits indefinitely. May be called only by a thread currently occupying this monitor.

waitForUninterruptiblyback to summary
public boolean waitForUninterruptibly(Monitor.Guard guard, Duration time)

Waits for the guard to be satisfied. Waits at most the given time. May be called only by a thread currently occupying this monitor.

Returns:boolean

whether the guard is now satisfied

Since
28.0
waitForUninterruptiblyback to summary
public boolean waitForUninterruptibly(Monitor.Guard guard, long time, TimeUnit unit)

Waits for the guard to be satisfied. Waits at most the given time. May be called only by a thread currently occupying this monitor.

Returns:boolean

whether the guard is now satisfied

Annotations
@SuppressWarnings:GoodTime
com.google.common.util.concurrent back to summary

public abstract Class Monitor.Guard

extends Object
Class Inheritance
Known Direct Subclasses
com.google.common.util.concurrent.ServiceManager.ServiceManagerState.AwaitHealthGuard, com.google.common.util.concurrent.ServiceManager.ServiceManagerState.StoppedGuard, com.google.common.util.concurrent.AbstractService.IsStartableGuard, com.google.common.util.concurrent.AbstractService.IsStoppableGuard, com.google.common.util.concurrent.AbstractService.HasReachedRunningGuard, com.google.common.util.concurrent.AbstractService.IsStoppedGuard

A boolean condition for which a thread may wait. A Guard is associated with a single Monitor. The monitor may check the guard at arbitrary times from any thread occupying the monitor, so code should not be written to rely on how often a guard might or might not be checked.

If a Guard is passed into any method of a Monitor other than the one it is associated with, an IllegalMonitorStateException is thrown.

Since
10.0

Field Summary

Modifier and TypeField and Description
pack-priv final Condition
pack-priv final Monitor
pack-priv Monitor.Guard
next

The next active guard

pack-priv int

Constructor Summary

AccessConstructor and Description
protected
Guard(Monitor monitor)

Method Summary

Modifier and TypeMethod and Description
public abstract boolean
isSatisfied()

Evaluates this guard's boolean condition.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

conditionback to summary
pack-priv final Condition condition
monitorback to summary
pack-priv final Monitor monitor
Annotations
@Weak
nextback to summary
pack-priv Monitor.Guard next

The next active guard

Annotations
@GuardedBy:monitor.lock
@CheckForNull
waiterCountback to summary
pack-priv int waiterCount
Annotations
@GuardedBy:monitor.lock

Constructor Detail

Guardback to summary
protected Guard(Monitor monitor)

Method Detail

isSatisfiedback to summary
public abstract boolean isSatisfied()

Evaluates this guard's boolean condition. This method is always called with the associated monitor already occupied. Implementations of this method must depend only on state protected by the associated monitor, and must not modify that state.