com.tomgibara.pronto.util
Class Waiter

java.lang.Object
  extended by com.tomgibara.pronto.util.Waiter

public class Waiter
extends java.lang.Object

An instance of this object can wait on an object's monitor until a condition is met, or a given number of milliseconds has elapsed - which ever occurs soonest. The class expects that it will be notified via the specified monitor when the condition may have changed. This class is safe for multithreaded use.

Author:
Tom Gibara

Nested Class Summary
static interface Waiter.Condition
          Implementations of this interface are supplied to a Waiter and control when the object may cease waiting.
 
Constructor Summary
Waiter(java.lang.Object monitor)
          Creates a new waiter for waiting on the supplied object.
 
Method Summary
 java.lang.Object getMonitor()
          The monitor on which this object will wait for changes in condition.
protected  boolean handleInterruption(java.lang.InterruptedException e)
          This method is called when an interruption occurs while waiting.
 boolean waitForCondition(long timeout, Waiter.Condition cond)
          This method will cause the calling thread to wait until the specified condtion is met or a timeout occurs, which ever is soonest.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Waiter

public Waiter(java.lang.Object monitor)
Creates a new waiter for waiting on the supplied object.

Parameters:
monitor - the object on which the waiter waits for a change in condition.
Method Detail

getMonitor

public java.lang.Object getMonitor()
The monitor on which this object will wait for changes in condition.

Returns:
the monitor on which this object will wait while waiting for a condition to be met.

waitForCondition

public boolean waitForCondition(long timeout,
                                Waiter.Condition cond)
This method will cause the calling thread to wait until the specified condtion is met or a timeout occurs, which ever is soonest. This class expects that the monitor will be notified when a condition is met. By default, this method will not abruptly in response to an interrupted exception, but this behavior may be modified by overriding the handleInterruption method.

Parameters:
timeout - the number of milliseconds to wait for, or 0 to wait indefinitely
cond - the condition for a timely return from this method
Returns:
true if the condition was met, false otherwise

handleInterruption

protected boolean handleInterruption(java.lang.InterruptedException e)
This method is called when an interruption occurs while waiting. The default implementation of this method returns true without performing any other action. The method may be overridden to change the response of this class to any interruptions that occur during waits.

Parameters:
e - the interrupting exception
Returns:
whether the object should continue to wait for the condition to be met


Copyright © 2006-2007 Tom Gibara. All Rights Reserved.