TimerTask is an abstract class defined in java.util package. TimerTask class defines a task that can be scheduled to run for just once or for repeated number of time. In order to define a TimerTask object, this class needs to be implemented and the run method need to be overriden. The run method is implicitly invoked when a timer object schdedules it to do so.
Note: An instance of TimerTask class is used to define a task the needs to run periodically.
Constructors:
- TimerTask(): Creates a new timer task
Declaration:
public abstract class TimerTask
extends Object
implements Runnable
Methods:
- cancel(): java.util.TimerTask.cancel() Cancels this timer task
Syntax:
public boolean cancel() 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.
- run(): java.util.TimerTask.run() The action to be performed by this timer task
Syntax:
public abstract void run() Description: The action to be performed by this timer task
- scheduledExecutionTime(): java.util.TimerTask.scheduledExecutionTime() Returns the scheduled execution time of the most recent actual execution of this task
Syntax:
public long scheduledExecutionTime() 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
Methods inherited from class java.lang.Object
- clone
- equals
- finalize
- getClass
- hashCode
- notify
- notifyAll
- toString
- wait
Java program to demonstrate usage of TimerTask class
// Java program to demonstrate // working of TimerTask class import java.util.Timer; import java.util.TimerTask; class Helper extends TimerTask { public static int i = 0; public void run() { System.out.println("Timer ran" + ++i); if(i == 4) { synchronized(Test.obj) { Test.obj.notify(); } } } } public class Test { public static Test obj; public static void main(String[] args) throws InterruptedException { obj = new Test(); // creating an instance of timer class Timer timer = new Timer(); // creating an instance of task to be scheduled TimerTask task = new Helper(); // scheduling the timer instance timer.schedule(task, 1000, 3000); // fetching the scheduled execution time of // the most recent actual execution of the task System.out.println(task.scheduledExecutionTime()); synchronized(obj) { //this thread waits until i reaches 4 obj.wait(); } //canceling the task assigned System.out.println("Cancel the timer task: " + task.cancel()); // at this point timer is still running // without any task assigned to it // canceling the timer instance created timer.cancel(); } } |
Output:
1495715853591 Timer ran 1 Timer ran 2 Timer ran 3 Timer ran 4 Cancel the timer task: true
Reference:
This article is contributed by Mayank Kumar. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
Recommended Posts:
- Java.lang.Class class in Java | Set 1
- Java.lang.Class class in Java | Set 2
- Using predefined class name as Class or Variable name in Java
- Java.util.TimeZone Class (Set-2) | Example On TimeZone Class
- Implement Pair Class with Unit Class in Java using JavaTuples
- Implement Triplet Class with Pair Class in Java using JavaTuples
- Implement Quintet Class with Quartet Class in Java using JavaTuples
- Implement Quartet Class with Triplet Class in Java using JavaTuples
- Implement Octet Class from Septet Class in Java using JavaTuples
- Implement Ennead Class from Octet Class in Java using JavaTuples
- Implement Sextet Class from Quintet Class in Java using JavaTuples
- Implement Septet Class from Sextet Class in Java using JavaTuples
- Implement Decade Class from Ennead Class in Java using JavaTuples
- Difference between Abstract Class and Concrete Class in Java
- In Java, Can we call the main() method of a class from another class?
- Does JVM create object of Main class (the class with main())?
- Inner Class And Anonymous Inner Class that Implements Runnable | Concurrent Programming Approach 3
- Java.util.BitSet class methods in Java with Examples | Set 2
- Java.Lang.Float class in Java
- Java.io.BufferedInputStream class in Java

