TimerTask
Stay organized with collections
Save and categorize content based on your preferences.
A task that can be scheduled for one-time or repeated execution by a Timer.
Protected Constructor Summary
Public Method Summary
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.
|
Inherited Method Summary
From class
java.lang.Object
Object
|
clone()
Creates and returns a copy of this Object .
|
boolean
|
equals( Object obj)
Compares this instance with the specified object and indicates if they
are equal.
|
void
|
finalize()
Invoked when the garbage collector has detected that this instance is no longer reachable.
|
final
Class<?>
|
getClass()
Returns the unique instance of Class that represents this
object's class.
|
int
|
hashCode()
Returns an integer hash code for this object.
|
final
void
|
notify()
Causes a thread which is waiting on this object's monitor (by means of
calling one of the wait() methods) to be woken up.
|
final
void
|
notifyAll()
Causes all threads which are waiting on this object's monitor (by means
of calling one of the wait() methods) to be woken up.
|
String
|
toString()
Returns a string containing a concise, human-readable description of this
object.
|
final
void
|
wait(long timeout, int nanos)
Causes the calling thread to wait until another thread calls the notify() or notifyAll() method of this object or until the
specified timeout expires.
|
final
void
|
wait(long timeout)
Causes the calling thread to wait until another thread calls the notify() or notifyAll() method of this object or until the
specified timeout expires.
|
final
void
|
wait()
Causes the calling thread to wait until another thread calls the notify() or notifyAll() method of this object.
|
From interface
java.lang.Runnable
abstract
void
|
run()
When an object implementing interface Runnable is used
to create a thread, starting the thread causes the object's
run method to be called in that separately executing
thread.
|
Protected Constructors
protected
TimerTask
()
Creates a new timer task.
Public Methods
public
boolean
cancel
()
Cancels this timer task. If the task has been scheduled for one-time
execution and has not yet run, or has not yet been scheduled, it will
never run. If the task has been scheduled for repeated execution, it
will never run again. (If the task is running when this call occurs,
the task will run to completion, but will never run again.)
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.
Returns
- 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 cancelled. (Loosely speaking, this method
returns true if it prevents one or more scheduled
executions from taking place.)
public
abstract
void
run
()
The action to be performed by this timer task.
public
long
scheduledExecutionTime
()
Returns the scheduled execution time of the most recent
actual execution of this task. (If this method is invoked
while task execution is in progress, the return value is the scheduled
execution time of the ongoing task execution.)
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.
Returns
- the time at which the most recent execution of this task was
scheduled to occur, in the format returned by Date.getTime().
The return value is undefined if the task has yet to commence
its first execution.
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2024-07-10 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2024-07-10 UTC."],[[["\u003cp\u003e\u003ccode\u003eTimerTask\u003c/code\u003e is an abstract class used for scheduling tasks for one-time or repeated execution by a \u003ccode\u003eTimer\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eIt provides methods for canceling the task (\u003ccode\u003ecancel()\u003c/code\u003e), defining the task's action (\u003ccode\u003erun()\u003c/code\u003e), and getting the scheduled execution time (\u003ccode\u003escheduledExecutionTime()\u003c/code\u003e).\u003c/p\u003e\n"],["\u003cp\u003eTasks are executed by a \u003ccode\u003eTimer\u003c/code\u003e and implement the \u003ccode\u003eRunnable\u003c/code\u003e interface, meaning they have a \u003ccode\u003erun()\u003c/code\u003e method that defines the task's logic.\u003c/p\u003e\n"],["\u003cp\u003e\u003ccode\u003escheduledExecutionTime()\u003c/code\u003e can be used within the \u003ccode\u003erun()\u003c/code\u003e method to determine if the task is running on schedule or if it's too late to perform its intended action.\u003c/p\u003e\n"]]],[],null,[]]