Class SearchTimer

  • All Implemented Interfaces:
    Runnable

    public class SearchTimer
    extends 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.
    Version:
    $id$
    Author:
    Matej Sekoranja
    • 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 - -- the command to run after the delay.
      • rescheduleAllAfterDelay

        public void rescheduleAllAfterDelay​(long millisecondsToDelay)
      • cancel

        public static void cancel​(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:
        ClassCastException - if the taskID argument is not of the type returned by an execute command.
      • getThread

        public 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.
      • hasNext

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