|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectjava.lang.Thread
edu.rit.util.TimerThread
public class TimerThread
Class TimerThread encapsulates a thread that does the timing for Timer
s and performs
TimerTask
s'
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 |
---|
public TimerThread()
Method Detail |
---|
public static TimerThread getDefault()
public Timer createTimer(TimerTask theTimerTask)
theTimerTask
- Timer task.
NullPointerException
- (unchecked exception) Thrown if theTimerTask is null.public void shutdown()
public void run()
run
in interface Runnable
run
in class Thread
IllegalStateException
- (unchecked exception) Thrown if some thread other than this timer
thread called the run() method.
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |