Package org.dellroad.stuff.java
Class TimedWait
java.lang.Object
org.dellroad.stuff.java.TimedWait
Utility class for performing timed waits on objects.
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionstatic boolean
Wait (usingObject.wait()
) up to a given time limit for some predicate to become true.
-
Method Details
-
wait
public static boolean wait(Object obj, long timeout, Predicate predicate) throws InterruptedException Wait (usingObject.wait()
) up to a given time limit for some predicate to become true. This method correctly handlesspurious 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 onobj
. It also guarantees that upon return,obj
will have remained continuously locked since the most recent invocation ofpredicate.test()
, so that any derived state computed therein will still be valid.This method uses
System.nanoTime()
instead ofSystem.currentTimeMillis()
and so is immune to adjustments in clock time.- Parameters:
obj
- object to sleep on; must already be lockedtimeout
- wait timeout in milliseconds, or zero for an infinite waitpredicate
- predicate to test- Returns:
- true if the predicate became true before the timeout, false if the timeout expired
- Throws:
IllegalArgumentException
- iftimeout
is negativeIllegalMonitorStateException
- ifobj
is not already lockedInterruptedException
- if the current thread is interrupted
-