Package jmri.util
Class WaitHandler
- java.lang.Object
-
- jmri.util.WaitHandler
-
public class WaitHandler extends java.lang.Object
Common utility class for handling the "spurious wakeup from wait()" problem documented inObject.wait(long)
. Generally, when waiting for a notify() operation, you need to provide a test that a valid notify had happened due to a state change or other .
By default, interrupting the thread leaves the wait early with the interrupted flag set. InterruptedException is not thrown. You can modify this behavior via the handleInterruptedException routine.new WaitHandler(this, 120) { protected boolean wasSpurious() { return !(state == expectedNextState); } };
-
-
Constructor Summary
Constructors Constructor Description WaitHandler(java.lang.Object self)
Wait forever, robustly handling "spurious wake"WaitHandler(java.lang.Object self, long interval)
Wait for a specified interval, robustly handling "spurious wake"
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description (package private) boolean
handleInterruptedException(java.lang.InterruptedException e)
Define interrupt processing.protected boolean
wasSpurious()
Method to determine if a wake was spurious or not.
-
-
-
Constructor Detail
-
WaitHandler
public WaitHandler(java.lang.Object self, long interval)
Wait for a specified interval, robustly handling "spurious wake"- Parameters:
self
- waiting Objectinterval
- in milliseconds
-
WaitHandler
public WaitHandler(java.lang.Object self)
Wait forever, robustly handling "spurious wake"- Parameters:
self
- waiting Object
-
-
Method Detail
-
wasSpurious
protected boolean wasSpurious()
Method to determine if a wake was spurious or not. By default, all wakes are considered not spurious and the full time may not elapse. Override to provide a test (returning true) when there's a way to tell that a wake was spurious and the wait() should continue.- Returns:
- false unless overridden by a subclass
-
handleInterruptedException
boolean handleInterruptedException(java.lang.InterruptedException e)
Define interrupt processing. By default, just records and leaves the wait early.- Parameters:
e
- the exception to handle- Returns:
- true if should break out of wait
-
-