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,["# TimerTask\n\npublic abstract class **TimerTask** extends [Object](../../../reference/java/lang/Object.html) \nimplements [Runnable](../../../reference/java/lang/Runnable.html) \nA task that can be scheduled for one-time or repeated execution by a Timer. \n\n##### See Also\n\n- [Timer](../../../reference/java/util/Timer.html) \n\n### Protected Constructor Summary\n\n|---|--------------------------------------------------------------------------------------------------|\n| | [TimerTask](../../../reference/java/util/TimerTask.html#TimerTask())() Creates a new timer task. |\n\n### Public Method Summary\n\n|---------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| boolean | [cancel](../../../reference/java/util/TimerTask.html#cancel())() Cancels this timer task. |\n| abstract void | [run](../../../reference/java/util/TimerTask.html#run())() The action to be performed by this timer task. |\n| long | [scheduledExecutionTime](../../../reference/java/util/TimerTask.html#scheduledExecutionTime())() Returns the *scheduled* execution time of the most recent *actual* execution of this task. |\n\n### Inherited Method Summary\n\nFrom class [java.lang.Object](../../../reference/java/lang/Object.html) \n\n|-------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| [Object](../../../reference/java/lang/Object.html) | [clone](../../../reference/java/lang/Object.html#clone())() Creates and returns a copy of this `Object`. |\n| boolean | [equals](../../../reference/java/lang/Object.html#equals(java.lang.Object))([Object](../../../reference/java/lang/Object.html) obj) Compares this instance with the specified object and indicates if they are equal. |\n| void | [finalize](../../../reference/java/lang/Object.html#finalize())() Invoked when the garbage collector has detected that this instance is no longer reachable. |\n| final [Class](../../../reference/java/lang/Class.html)\\\u003c?\\\u003e | [getClass](../../../reference/java/lang/Object.html#getClass())() Returns the unique instance of [Class](../../../reference/java/lang/Class.html) that represents this object's class. |\n| int | [hashCode](../../../reference/java/lang/Object.html#hashCode())() Returns an integer hash code for this object. |\n| final void | [notify](../../../reference/java/lang/Object.html#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. |\n| final void | [notifyAll](../../../reference/java/lang/Object.html#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. |\n| [String](../../../reference/java/lang/String.html) | [toString](../../../reference/java/lang/Object.html#toString())() Returns a string containing a concise, human-readable description of this object. |\n| final void | [wait](../../../reference/java/lang/Object.html#wait(long,%20int))(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. |\n| final void | [wait](../../../reference/java/lang/Object.html#wait(long))(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. |\n| final void | [wait](../../../reference/java/lang/Object.html#wait())() Causes the calling thread to wait until another thread calls the `notify()` or `notifyAll()` method of this object. |\n\nFrom interface [java.lang.Runnable](../../../reference/java/lang/Runnable.html) \n\n|---------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| abstract void | [run](../../../reference/java/lang/Runnable.html#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. |\n\nProtected Constructors\n----------------------\n\n#### protected\n**TimerTask**\n()\n\nCreates a new timer task.\n\nPublic Methods\n--------------\n\n#### public boolean\n**cancel**\n()\n\nCancels this timer task. If the task has been scheduled for one-time\nexecution and has not yet run, or has not yet been scheduled, it will\nnever run. If the task has been scheduled for repeated execution, it\nwill never run again. (If the task is running when this call occurs,\nthe task will run to completion, but will never run again.)\n\nNote that calling this method from within the run method of\na repeating timer task absolutely guarantees that the timer task will\nnot run again.\n\nThis method may be called repeatedly; the second and subsequent\ncalls have no effect. \n\n##### Returns\n\n- 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.) \n\n#### public abstract void\n**run**\n()\n\nThe action to be performed by this timer task. \n\n#### public long\n**scheduledExecutionTime**\n()\n\nReturns the *scheduled* execution time of the most recent\n*actual* execution of this task. (If this method is invoked\nwhile task execution is in progress, the return value is the scheduled\nexecution time of the ongoing task execution.)\n\nThis method is typically invoked from within a task's run method, to\ndetermine whether the current execution of the task is sufficiently\ntimely to warrant performing the scheduled activity: \n\n public void run() {\n if (System.currentTimeMillis() - scheduledExecutionTime() \u003e=\n MAX_TARDINESS)\n return; // Too late; skip this execution.\n // Perform the task\n }\n \nThis 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.\n\n\u003cbr /\u003e\n\n##### Returns\n\n- 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. \n\n##### See Also\n\n- [Date.getTime()](../../../reference/java/util/Date.html#getTime())"]]