Runnable
public abstract class TimerTask extends Object implements Runnable
Timer
. A timer task is not reusable. Once a task has been scheduled for execution on a Timer
or cancelled, subsequent attempts to schedule it for execution will throw IllegalStateException
.
Modifier | Constructor | Description |
---|---|---|
protected |
Creates a new timer task. |
Modifier and Type | Method | 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. |
protected TimerTask()
public abstract void run()
public boolean cancel()
Note that calling this method from within the run
method of a repeating timer task absolutely guarantees that the timer task will not run again.
This method may be called repeatedly; the second and subsequent calls have no effect.
true
if it prevents one or more scheduled executions from taking place.)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 (System.currentTimeMillis() - 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.
© 1993, 2023, Oracle and/or its affiliates. All rights reserved.
Documentation extracted from Debian's OpenJDK Development Kit package.
Licensed under the GNU General Public License, version 2, with the Classpath Exception.
Various third party code in OpenJDK is licensed under different licenses (see Debian package).
Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/util/TimerTask.html