AppDividend
Latest Code Tutorials

Timer Class in Java Example | Java Timer Class Tutorial

0

Timer Class in Java Example | Java Timer Class Tutorial is today’s topic. In programming, there are many tasks which 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 which 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 which 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.

#Default Constructor

#Timer()

The following constructor is used to create a new timer whenever an object of Timer class is created with no arguments passed to it.

#Timer(boolean isDaemon)

Here, a new timer is created whose thread may be working as a Daemon. If this is so, the isDaemon returns a true value.

#Timer(String s)

This is used to create a timer whose associated thread has a name specified.

#Timer(String s, boolean isDaemon)

In this case, a timer class is created whose associated String has a name specified, and the thread may be working as a Daemon thread.

The packages to be included while working with the timer class is following.

java.util.Timer;

And for the task which needs to be scheduled, we use the following package.

java.util.TimerTask;

Now, let us have a look at the various schedule methods which belong to the Timer class.

Note: All these schedule methods are public, and they do not return any value.

#Timer Class Methods

#schedule(TimerTask task, Date time)

The scheduling method takes in two arguments: the first one is the task which needs to be scheduled, and the second one is the time at which the task needs to be scheduled to be executed. 

This method is used in cases where the task is to be scheduled only once.

#schedule(TimerTask task, Date firstTime, long period)

This method takes three arguments: the first again being the task which needs to be scheduled, the other is the Time at which the task needs to be executed for the first time, and the third argument is the time period after which the task needs to be executed again.

This method is used in cases where the task is to be executed repeatedly after a fixed time period.

#schedule(TimerTask task, long delay)

In this method, the task is scheduled after a particular time delay.

#schedule(TimerTask task, long delay, long period)

This method takes three arguments, in which the first one is the task that needs to be scheduled, the second one is the delay before the first execution of the task, and the third argument is the time in milliseconds which should be there between every single execution of the task.

Let us have a look at the following example which prints the message to remind the user to drink water.

import java.util.Timer;
import java.util.TimerTask;

class Task_eg extends TimerTask
{
    public void run() {
        System.out.println("Drink Water, its been a while");
    }
}
public class Example 
{
    public static void main(String[] args)
    {
        Timer timer = new Timer();
        TimerTask task = new Task_eg();

        timer.schedule(task, 3600, 5000);
    }
}

See the following output.

 

Timer Class in Java Example

Apart from these, the following are various scheduling methods which execute a task for a fixed rate.

#scheduleAtFixedRate(TimerTask task, Date firstTime, long period)

The scheduleAtFixedRate() method is similar to the following schedule method:

schedule(TimerTask task,Date firstTime,long period)

The only difference is that the schedules are specified to execute repeatedly for a fixed-rate of time.

#scheduleAtFixedRate(TimerTask task, long delay, long period)

The scheduleAtFixedRate() method is also similar to the following schedule method: schedule(TimerTask task, long delay, long period) and takes the same types of arguments. The difference is again in the fixed rate of repeated execution of the scheduled tasks.

There are also some more methods apart from the schedule and scheduleAtFixedRate methods that are inherited from the Object class and play an important role in scheduling tasks. A few of them are as follows:

#cancel()

The java.util.Timer.cancel() method terminates the Timer and discards all the scheduled tasks. However, the currently executing task is not interfered in between.

See the following syntax.

public void cancel()

#purge()

The java.util.Timer.purge()  removes all the canceled tasks from the timers queue and returns the number of tasks that it has removed.

See the following syntax.

public int purge()

Finally, Timer Class in Java Example | Java Timer Class Tutorial is over.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.