Package jmri.util

Class WaitHandler


  • public class WaitHandler
    extends java.lang.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 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.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • WaitHandler

        public WaitHandler​(java.lang.Object self,
                           long interval)
        Wait for a specified interval, robustly handling "spurious wake"
        Parameters:
        self - waiting Object
        interval - 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