Class TimedWait

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

public final class TimedWait extends Object
Utility class for performing timed waits on objects.
See Also:
  • Method Details

    • 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. It also guarantees that upon return, obj will have remained continuously locked since the most recent invocation of predicate.test(), so that any derived state computed therein will still be valid.

      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