Java.util.TimerTask class in Java Last Updated : 26 Apr, 2022 Comments Improve Suggest changes Like Article Like Report 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 overridden. The run method is implicitly invoked when a timer object schedules 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 taskscheduledExecutionTime(): 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 executionMethods inherited from class java.lang.Object cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitJava program to demonstrate usage of TimerTask class Java // 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: Oracle Comment More infoAdvertise with us Next Article Java.util.TimerTask class in Java M Mayank Kumar Improve Article Tags : Misc Java Practice Tags : JavaMisc Similar Reads Java.util.Timer Class in Java Timer class provides a method call that is used by a thread to schedule a task, such as running a block of code after some regular instant of time. Each task may be scheduled to run once or for a repeated number of executions. Each timer object is associated with a background thread that is responsi 6 min read java.time.LocalTime Class in Java Java is the most popular programming language and widely used programming language. Java is used in all kinds of applications like mobile applications, desktop applications, web applications. As in Java, java.time.LocalTime class represents time, which is viewed as hour-minute-second. This class is 5 min read java.time.OffsetTime Class in Java Java OffsetTime class is an immutable date-time object that represents a time, often viewed as hour-minute-second offset. OffsetTime class represents a time with an offset from UTC/Greenwich in the ISO-8601 calendar system, such as 18:30:45+08:00, often viewed as an hour-minute-second-offset. This c 7 min read Java.lang.Runtime class in Java In Java, the Runtime class is used to interact with Every Java application that has a single instance of class Runtime that allows the application to interface with the environment in which the application is running. The current runtime can be obtained from the getRuntime() method. Methods of Java 6 min read java.time.Clock Class in Java Java Clock class is present in java.time package. It was introduced in Java 8 and provides access to current instant, date, and time using a time zone. The use of the Clock class is not mandatory because all date-time classes also have a now() method that uses the system clock in the default time zo 3 min read java.time.LocalDateTime Class in Java java.time.LocalDateTime class, introduced in Java 8, represents a local date-time object without timezone information. The LocalDateTime class in Java is an immutable date-time object that represents a date in the yyyy-MM-dd-HH-mm-ss.zzz format. It implements the ChronoLocalDateTime interface and in 4 min read java.time.OffsetDateTime Class in Java Java is the most popular programming language and widely used programming language. Java is used in all kind of application like mobile application, desktop application, web application. In this Java java.time.OffsetDate, Time class is imported which is an immutable representation of a date-time wit 4 min read java.time.LocalDate Class in Java Java is the most popular programming language and widely used programming language. Java is used in all kind of application like as mobile application, desktop application, web application. In this Java java.time.LocalDate class is imported which represents to display the current date. java.time: It 4 min read java.time.Instant Class in Java In Java language, the Instant Class is used to represent the specific time instant on the current timeline. The Instant Class extends the Object Class and implements the Comparable interface. Declaration of the Instant Class public final class Instant extends Object implements Temporal, TemporalAdju 4 min read java.time.Duration Class in Java Duration is the value-based Class present in the Java time library. It's used to get time-based amount of time. This class is immutable and thread-safe. This article describes all the methods present in this class and some basic examples using class methods. This class implements Serializable, Comp 4 min read Like