Package jmri.util

Class WaitHandler

java.lang.Object
jmri.util.WaitHandler

public class WaitHandler extends Object
Common utility class for handling the "spurious wakeup from wait()" problem documented in Object.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 .

 new WaitHandler(this, 120) {
 protected boolean wasSpurious() {
 return !(state == expectedNextState);
 }
 };
 
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.
  • Constructor Details

    • WaitHandler

      public WaitHandler(Object self, long interval)
      Wait for a specified interval, robustly handling "spurious wake"
      Parameters:
      self - waiting Object
      interval - in milliseconds
    • WaitHandler

      public WaitHandler(Object self)
      Wait forever, robustly handling "spurious wake"
      Parameters:
      self - waiting Object
  • Method Details

    • 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

      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