org.dellroad.stuff.java
Class TimedWait

java.lang.Object
  extended by org.dellroad.stuff.java.TimedWait

public final class TimedWait
extends Object

Utility class for performing timed waits on objects.

See Also:
Object.wait(long)

Method Summary
static boolean wait(Object obj, long timeout, Predicate predicate)
          Wait (using Object.wait()) up to a given time limit for some predicate to become true.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

wait

public static boolean wait(Object obj,
                           long timeout,
                           Predicate predicate)
                    throws InterruptedException
Wait (using Object.wait()) up to a given time limit for some predicate to become true. This method correctly handles spurious wakeups, restarting the wait loop as necessary.

This method assumes that obj will be notified whent the predicate becomes true and that the current thread is already synchronized on obj.

This method uses System.nanoTime() instead of System.currentTimeMillis() and so is immune to adjustments in clock time.

Parameters:
obj - object to sleep on; must already be locked
timeout - wait timeout in milliseconds, or zero for an infinite wait
predicate - predicate to test
Returns:
true if the predicate became true before the timeout, false if the timeout expired
Throws:
IllegalArgumentException - if timeout is negative
IllegalMonitorStateException - if obj is not already locked
InterruptedException - if the current thread is interrupted