Modifier | Constructor and Description |
---|---|
protected |
TimerTask()
Creates a new timer task.
|
Modifier and Type | Method and Description |
---|---|
boolean |
cancel()
Cancels this timer task.
|
abstract void |
run()
The action to be performed by this timer task.
|
long |
scheduledExecutionTime()
Returns the scheduled execution time of the most recent actual execution of
this task.
|
void |
uncaughtException(Timer timer,
Throwable e)
|
public boolean cancel()
true
if this task is scheduled for one-time execution
and has not yet run, or this task is scheduled for repeated
execution. Returns false
if the task was scheduled for
one-time execution and has already run, or if the task was never
scheduled, or if the task was already canceled (Loosely speaking,
this method returns true
if it prevents one or more
scheduled executions from taking place.)public abstract void run()
run
in interface Runnable
Thread.run()
public long scheduledExecutionTime()
This method is typically invoked from within a task's run method, to determine whether the current execution of the task is sufficiently timely to warrant performing the scheduled activity:
public void run() { if (CurrentTime.get() - scheduledExecutionTime() >= MAX_TARDINESS) return; // Too late; skip this execution. // Perform the task }
This method is typically not used in conjunction with fixed-delay execution repeating tasks, as their scheduled execution times are allowed to drift over time, and so are not terribly significant.
Date.getTime()
public void uncaughtException(Timer timer, Throwable e)
TimerTask
terminates due to an uncaught
exception thrown by run()
.
The default implementation of this method handles the uncaught exception as following:
Timer
instance has a registered handler, this handler is
invoked,Timer
class has a registered default handler,
this handler is invoked,Throwable.printStackTrace()
is invoked on the given exception.
Any exception thrown by this method will be ignored.
timer
- The Timer
on which this TimerTask
is scheduled.e
- The uncaught exception.Timer.setDefaultUncaughtExceptionHandler(Thread.UncaughtExceptionHandler)
,
Timer.setUncaughtExceptionHandler(Thread.UncaughtExceptionHandler)