Class TimerThread
- All Implemented Interfaces:
Runnable
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
-
Nested Class Summary
Nested classes/interfaces inherited from class java.lang.Thread
Thread.Builder, Thread.State, Thread.UncaughtExceptionHandler
-
Field Summary
Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptioncreateTimer
(TimerTask theTimerTask) Create a new timer associated with the given timer task and under the control of this timer thread.static TimerThread
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, clone, countStackFrames, currentThread, dumpStack, enumerate, getAllStackTraces, getContextClassLoader, getDefaultUncaughtExceptionHandler, getId, getName, getPriority, getStackTrace, getState, getThreadGroup, getUncaughtExceptionHandler, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, isVirtual, join, join, join, join, ofPlatform, ofVirtual, onSpinWait, resume, setContextClassLoader, setDaemon, setDefaultUncaughtExceptionHandler, setName, setPriority, setUncaughtExceptionHandler, sleep, sleep, sleep, start, startVirtualThread, stop, suspend, threadId, toString, yield
-
Constructor Details
-
TimerThread
public TimerThread()Construct a new timer thread. After constructing it, you must call the timer thread'sstart()
method, or no timeouts will occur.
-
-
Method Details
-
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 toTimerThread.getDefault()
.- Returns:
- Default timer thread.
-
createTimer
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'saction()
method.- Parameters:
theTimerTask
- Timer task.- Returns:
- a
Timer
object. - Throws:
NullPointerException
- (unchecked exception) Thrown iftheTimerTask
is null.
-
shutdown
public void shutdown()Shut down this timer thread. -
run
public void run()Perform this timer thread's processing. (Never call therun()
method yourself!)- Specified by:
run
in interfaceRunnable
- Overrides:
run
in classThread
- Throws:
IllegalStateException
- (unchecked exception) Thrown if some thread other than this timer thread called therun()
method.
-