Package edu.rit.util

Class TimerThread

java.lang.Object
java.lang.Thread
edu.rit.util.TimerThread
All Implemented Interfaces:
Runnable

public class TimerThread extends Thread
Class TimerThread encapsulates a thread that does the timing for Timers and performs TimerTasks' actions when timeouts occur.

A timer is created by calling a timer thread's createTimer() method, giving a timer task to associate with the timer. Multiple timers may be created under the control of the same timer thread. The timer thread will perform the actions of its timers' timer tasks one at a time at the proper instants (by causing each timer to call its timer task's action() method). Note that the timer tasks of a single timer thread are performed sequentially, which may cause some timer tasks' actions to be delayed depending on how long other timer tasks' actions take to execute. If necessary, consider running separate timers in separate timer threads so the timer tasks' actions run concurrently.

Class TimerThread does not offer real-time guarantees. It merely makes a best-effort attempt to perform each timer task's actions as soon as possible after the timeouts occur.

A TimerThread is just a Thread. After constructing a timer thread, you can mark it as a daemon thread if you want. You must also call the timer thread's start() method, or no timeouts will occur. To gracefully stop a timer thread, call the shutdown() method.

To simplify writing programs with multiple objects that all use the same timer thread, the static TimerThread.getDefault() method returns a single shared instance of class TimerThread. The default timer thread is marked as a daemon thread and is started automatically. The default timer thread is not created until the first call to TimerThread.getDefault().

Classes Timer, TimerTask, and TimerThread provide capabilities similar to classes java.util.Timer and java.util.TimerTask. Unlike the latter, they also provide the ability to stop and restart a timer and the ability to deal with race conditions in multithreaded programs.

Version:
18-Jun-2003
Author:
Alan Kaminsky
  • Constructor Details

    • TimerThread

      public TimerThread()
      Construct a new timer thread. After constructing it, you must call the timer thread's start() method, or no timeouts will occur.
  • Method Details

    • getDefault

      public static TimerThread getDefault()
      Get the default timer thread, a single shared instance of class TimerThread. The default timer thread is marked as a daemon thread and is started automatically. The default timer thread is not created until the first call to TimerThread.getDefault().
      Returns:
      Default timer thread.
    • createTimer

      public Timer createTimer(TimerTask theTimerTask)
      Create a new timer associated with the given timer task and under the control of this timer thread. When the timer is triggered, this timer thread will cause the timer to call the given timer task's action() method.
      Parameters:
      theTimerTask - Timer task.
      Returns:
      a Timer object.
      Throws:
      NullPointerException - (unchecked exception) Thrown if theTimerTask is null.
    • shutdown

      public void shutdown()
      Shut down this timer thread.
    • run

      public void run()
      Perform this timer thread's processing. (Never call the run() method yourself!)
      Specified by:
      run in interface Runnable
      Overrides:
      run in class Thread
      Throws:
      IllegalStateException - (unchecked exception) Thrown if some thread other than this timer thread called the run() method.