edu.rice.cs.plt.concurrent
Class CompletionMonitor

java.lang.Object
  extended by edu.rice.cs.plt.concurrent.CompletionMonitor
All Implemented Interfaces:
Condition

public class CompletionMonitor
extends Object
implements Condition

Provides a convenient facility for blocking until a boolean flag is signaled. Typically, this is used for communication in which one thread must complete a task before other threads can proceed.


Constructor Summary
CompletionMonitor()
          Create an unsignaled completion monitor.
CompletionMonitor(boolean signaled)
          Create a completion monitor in the given initial state.
 
Method Summary
 boolean attemptEnsureSignaled()
          Tries to ensure that the monitor has been signaled before continuing.
 boolean attemptEnsureSignaled(long timeout)
          Tries to ensure that the monitor has been signaled before continuing.
 boolean attemptEnsureSignaled(long timeout, TimeUnit unit)
          Tries to ensure that the monitor has been signaled before continuing.
 void ensureSignaled()
          Ensures that the monitor has been signaled before continuing.
 void ensureSignaled(long timeout)
          Ensures that the monitor has been signaled before continuing.
 void ensureSignaled(long timeout, TimeUnit unit)
          Ensures that the monitor has been signaled before continuing.
 boolean isSignaled()
          Returns whether the flag is currently set
 boolean isTrue()
          Returns whether the flag is currently set
 void reset()
          Revert to the unsignaled state
 void signal()
          Sets the state to signaled and alerts all blocked threads
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

CompletionMonitor

public CompletionMonitor()
Create an unsignaled completion monitor.


CompletionMonitor

public CompletionMonitor(boolean signaled)
Create a completion monitor in the given initial state. If signaled is true, invocations of ensureSignaled() will not block until reset() is invoked.

Method Detail

isSignaled

public boolean isSignaled()
Returns whether the flag is currently set


isTrue

public boolean isTrue()
Returns whether the flag is currently set

Specified by:
isTrue in interface Condition

reset

public void reset()
Revert to the unsignaled state


signal

public void signal()
Sets the state to signaled and alerts all blocked threads


ensureSignaled

public void ensureSignaled()
                    throws InterruptedException
Ensures that the monitor has been signaled before continuing. Blocks if necessary.

Throws:
InterruptedException

ensureSignaled

public void ensureSignaled(long timeout)
                    throws InterruptedException,
                           TimeoutException
Ensures that the monitor has been signaled before continuing. Blocks if necessary; fails if the the timeout is reached.

Parameters:
timeout - Maximum wait time, in milliseconds.
Throws:
InterruptedException
TimeoutException

ensureSignaled

public void ensureSignaled(long timeout,
                           TimeUnit unit)
                    throws InterruptedException,
                           TimeoutException
Ensures that the monitor has been signaled before continuing. Blocks if necessary; fails if the the timeout is reached.

Parameters:
timeout - Maximum wait time, in unit units.
unit - Units for timeout.
Throws:
InterruptedException
TimeoutException

attemptEnsureSignaled

public boolean attemptEnsureSignaled()
Tries to ensure that the monitor has been signaled before continuing. Blocks if necessary. If the wait is interrupted, returns false.


attemptEnsureSignaled

public boolean attemptEnsureSignaled(long timeout)
Tries to ensure that the monitor has been signaled before continuing. Blocks if necessary. If the wait is interrupted or the timeout is reached, returns false.

Parameters:
timeout - Maximum wait time, in milliseconds.

attemptEnsureSignaled

public boolean attemptEnsureSignaled(long timeout,
                                     TimeUnit unit)
Tries to ensure that the monitor has been signaled before continuing. Blocks if necessary. If the wait is interrupted or the timeout is reached, returns false.

Parameters:
timeout - Maximum wait time, in unit units.
unit - Units for timeout.