Class SearchTimer

  • All Implemented Interfaces:
    java.lang.Runnable

    public class SearchTimer
    extends java.lang.Thread
    Timer. Based on EDU.oswego.cs.dl.util.concurrent. Timer tasks should complete quickly. If a timer task takes excessive time to complete, it "hogs" the timer's task execution thread. This can, in turn, delay the execution of subsequent tasks, which may "bunch up" and execute in rapid succession when (and if) the offending task finally completes.
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      class  SearchTimer.Heap
      A heap-based priority queue.
      protected class  SearchTimer.RunLoop
      The run loop is isolated in its own Runnable class just so that the main class need not implement Runnable, which would allow others to directly invoke run, which is not supported.
      static class  SearchTimer.TimerTask  
      • Nested classes/interfaces inherited from class java.lang.Thread

        java.lang.Thread.State, java.lang.Thread.UncaughtExceptionHandler
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected SearchTimer.Heap heap
      Tasks are maintained in a standard priority queue.
      protected SearchTimer.RunLoop runLoop  
      protected java.lang.Thread thread
      The thread used to process commands
      • Fields inherited from class java.lang.Thread

        MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
    • Constructor Summary

      Constructors 
      Constructor Description
      SearchTimer()
      Protected constructor (singleton pattern).
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      static void cancel​(java.lang.Object taskID)
      Cancel a scheduled task that has not yet been run.
      protected void clearThread()
      set thread to null to indicate termination
      void executeAfterDelay​(long millisecondsToDelay, SearchTimer.TimerTask task)
      Execute the given command after waiting for the given delay.
      java.lang.Thread getThread()  
      boolean hasNext​(long dT)
      Check whether there is a task scheduled in next "dT" ms.
      protected SearchTimer.TimerTask nextTask​(boolean blockAndExtract, long dt)
      Return the next task to execute, or null if thread is interrupted.
      void rescheduleAllAfterDelay​(long millisecondsToDelay)  
      void restart()
      Start (or restart) a thread to process commands, or wake up an existing thread if one is already running.
      void shutDown()
      Cancel all tasks and interrupt the background thread executing the current task, if any.
      • Methods inherited from class java.lang.Object

        equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
      • Methods inherited from class java.lang.Thread

        activeCount, checkAccess, clone, countStackFrames, currentThread, destroy, dumpStack, enumerate, getAllStackTraces, getContextClassLoader, getDefaultUncaughtExceptionHandler, getId, getName, getPriority, getStackTrace, getState, getThreadGroup, getUncaughtExceptionHandler, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, onSpinWait, resume, run, setContextClassLoader, setDaemon, setDefaultUncaughtExceptionHandler, setName, setPriority, setUncaughtExceptionHandler, sleep, sleep, start, stop, stop, suspend, toString, yield
    • Field Detail

      • heap

        protected final SearchTimer.Heap heap
        Tasks are maintained in a standard priority queue.
      • thread

        protected java.lang.Thread thread
        The thread used to process commands
    • Constructor Detail

      • SearchTimer

        public SearchTimer()
        Protected constructor (singleton pattern).
    • Method Detail

      • executeAfterDelay

        public void executeAfterDelay​(long millisecondsToDelay,
                                      SearchTimer.TimerTask task)
        Execute the given command after waiting for the given delay.
        Parameters:
        millisecondsToDelay - -- the number of milliseconds from now to run the command.
        task - -- timer task
      • rescheduleAllAfterDelay

        public void rescheduleAllAfterDelay​(long millisecondsToDelay)
      • cancel

        public static void cancel​(java.lang.Object taskID)
        Cancel a scheduled task that has not yet been run. The task will be cancelled upon the next opportunity to run it. This has no effect if this is a one-shot task that has already executed. Also, if an execution is in progress, it will complete normally. (It may however be interrupted via getThread().interrupt()). But if it is a periodic task, future iterations are cancelled.
        Parameters:
        taskID - -- a task reference returned by one of the execute commands
        Throws:
        java.lang.ClassCastException - if the taskID argument is not of the type returned by an execute command.
      • getThread

        public java.lang.Thread getThread()
      • clearThread

        protected void clearThread()
        set thread to null to indicate termination
      • restart

        public void restart()
        Start (or restart) a thread to process commands, or wake up an existing thread if one is already running. This method can be invoked if the background thread crashed due to an unrecoverable exception in an executed command.
      • shutDown

        public void shutDown()
        Cancel all tasks and interrupt the background thread executing the current task, if any. A new background thread will be started if new execution requests are encountered. If the currently executing task does not respond to interrupts, the current thread may persist, even if a new thread is started via restart().
      • nextTask

        protected SearchTimer.TimerTask nextTask​(boolean blockAndExtract,
                                                 long dt)
        Return the next task to execute, or null if thread is interrupted.
        Parameters:
        blockAndExtract - block and extract
        dt - dt
        Returns:
        the timer task.
      • hasNext

        public boolean hasNext​(long dT)
        Check whether there is a task scheduled in next "dT" ms.
        Parameters:
        dT - dT
        Returns:
        boolean