public class ProtectedTimer
extends java.lang.Object
java.util.Timer
, tasks are scheduled for future execution in a
background thread, either for one-time execution, or repeated execution at
regular intervals.
Each ProtectedTimer
object launches a single background
thread on which all of the timer's tasks are executed sequentially. If
tasks are not scheduled to repeat, schedulingDone()
should be
invoked after all required tasks has been scheduled. This will cause the
background thread stop after all tasks has been completed or cancelled.
Otherwise the background thread will continue running indefinitely (for a
non-daemon thread) or until the JVM exits (for a daemon thread).
This implementation is protected from out of memory errors. If an
OutOfMemoryError
is thrown on the background thread, it is
caught and logged if possible, but processing is allowed to continue.
If a task is running at the time of the error, there is no guarantee it has
completed its work (protected tasks themselves should handle out of memory
errors in a way appropriate to their implementations). The existing
schedule is honored, however (to the extent possible), since the background
thread keeps running.
This class is thread-safe: multiple threads can share a single
ProtectedTimer
object without the need for external
synchronization.
Modifier and Type | Class and Description |
---|---|
private class |
ProtectedTimer.TimerThread
Background timer thread.
|
Modifier and Type | Field and Description |
---|---|
private static org.apache.commons.logging.Log |
LOG
Logger
|
private java.lang.String |
oomeMessage
Error message to be logged in the event of OutOfMemoryError
|
private java.util.concurrent.DelayQueue<ProtectedTimerTask> |
queue
Queue of the scheduled tasks.
|
private static int |
timerNumber
The variable used to generate sequential timer numbers.
|
private ProtectedTimer.TimerThread |
timerThread
Background thread for running scheduled tasks.
|
Constructor and Description |
---|
ProtectedTimer()
Constructs a protected timer with non-daemon background thread.
|
ProtectedTimer(boolean isDaemon)
Constructs a protected timer.
|
ProtectedTimer(java.lang.String threadName,
boolean isDaemon)
Constructs a protected timer.
|
Modifier and Type | Method and Description |
---|---|
void |
cancel()
Immediately stops all scheduled tasks and background timer thread.
|
private static int |
getNextTimerNumber()
Return next sequential unique timer number.
|
void |
schedule(ProtectedTimerTask task,
long initialDelay)
Schedules a non-repeating task to the timer.
|
void |
schedule(ProtectedTimerTask task,
long initialDelay,
long periodicalDelay)
Schedules a repeating task to the timer.
|
void |
schedulingDone()
Should be called if the background timer thread should exit after all
scheduled tasks has been completed or cancelled.
|
private static final org.apache.commons.logging.Log LOG
private static int timerNumber
private final ProtectedTimer.TimerThread timerThread
private final java.lang.String oomeMessage
private java.util.concurrent.DelayQueue<ProtectedTimerTask> queue
public ProtectedTimer()
public ProtectedTimer(boolean isDaemon)
isDaemon
- true
if the background timer thread should be run
as a daemon.public ProtectedTimer(java.lang.String threadName, boolean isDaemon)
threadName
- Name of the timer's background thread.isDaemon
- true
if the background timer thread should be run
as a daemon.private static int getNextTimerNumber()
public void schedule(ProtectedTimerTask task, long initialDelay)
task
- Task to be scheduled.initialDelay
- Delay before execution of the task.public void schedule(ProtectedTimerTask task, long initialDelay, long periodicalDelay)
task
- Task to be scheduled.initialDelay
- Delay before execution of the task.periodicalDelay
- Interval between the subsequent executions of the task.public void cancel()
public void schedulingDone()