In programming, there are many tasks that must be scheduled either to run once or for a repeated number of times. In Java, this can be achieved with the help of the Timer class. The Timer class calls a method that is responsible for scheduling any task. Various threads can use this method, and thus the Timer class is said to be thread-safe.

Timer Class in Java

Timer class is thread-safe. Timer class uses a binary heap data structure to store its task. The Timer class uses a binary heap data structure for storing the tasks. The binary heap is a complete binary tree that satisfies the property of heap order. Each node of this binary tree represents a task in the Timer class.

The Timer class, like all the other classes, inherits from the  Object class. Therefore, the declaration of the Timer class is as follows:

public class Timer extends Object

But, by default, as every class inherits from the Object class, there is no need to mention the Object class in is a declaration. Therefore, It is okay to write the above code as follows.

public class Timer  

Now, whenever we create an object of the Timer class, its constructor will be called. The Timer class contains overloaded constructors in its body. Let us have a look at the types of  constructors of the Timer class.

#java #programming

Timer Class in Java Example | Java Timer Class Tutorial
1.25 GEEK