edu.rit.util
Class TimerThread

java.lang.Object
  extended by java.lang.Thread
      extended by 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.


Nested Class Summary
 
Nested classes/interfaces inherited from class java.lang.Thread
Thread.State, Thread.UncaughtExceptionHandler
 
Field Summary
 
Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
 
Constructor Summary
TimerThread()
          Construct a new timer thread.
 
Method Summary
 Timer createTimer(TimerTask theTimerTask)
          Create a new timer associated with the given timer task and under the control of this timer thread.
static TimerThread getDefault()
          Get the default timer thread, a single shared instance of class TimerThread.
 void run()
          Perform this timer thread's processing.
 void shutdown()
          Shut down this timer thread.
 
Methods inherited from class java.lang.Thread
activeCount, checkAccess, countStackFrames, currentThread, destroy, dumpStack, enumerate, getAllStackTraces, getContextClassLoader, getDefaultUncaughtExceptionHandler, getId, getName, getPriority, getStackTrace, getState, getThreadGroup, getUncaughtExceptionHandler, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, resume, setContextClassLoader, setDaemon, setDefaultUncaughtExceptionHandler, setName, setPriority, setUncaughtExceptionHandler, sleep, sleep, start, stop, stop, suspend, toString, yield
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

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 Detail

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.
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.


Copyright © 2001-2006 by Alan Kaminsky. All rights reserved. Send comments to ark­@­cs.rit.edu.