Friday, May 26, 2023

What is Timer and TimerTask in Java – Tutorial Example

Timer in Java is a utility class that is used to schedule tasks for both one time and repeated execution. Timer is similar to the alarm facility many people use in mobile phones. Just like you can have one time alarm or repeated alarm, You can use java.util.Timer to schedule a time task or repeated task. In fact, we can implement a Reminder utility using Timer in Java and that's what we are going to see in this example of Timer in Java. Two classes java.util.Timer and java.util.TimerTask is used to schedule jobs in Java and forms Timer API. The TimerTask is an actual task that is executed by Timer. Similar to Thread in JavaTimerTask also implements the Runnable interface and overrides run method to specify a task details. 


This Java tutorial will also highlight the difference between Timer and TimerTask class and explains how Timer works in Java. By the way, the difference between Timer and Thread is also a popular Java questions on fresher-level interviews.



What is Timer and TimerTask in Java

Timer in Java is a utility class from java.util package which provides the facility to schedule tasks at any time in the future. As I said earlier, Timer is analog to the alarm clock you set up on your smartphone. 

Just like alarm can be either one time or recurring, You can also schedule task for one time and recurring time interval using Timer API. Timer provides method to schedule Task where the task is an instance of TimerTask class, which implements the Runnable interface and overrides run() method to define task which is called on scheduled time.



How Timer works in Java?

Timer class in Java maintains a background Thread (this could be either daemon thread or user thread, based on how you created your Timer object), also called timer's task execution thread. For each Timer, there would be a corresponding task processing Thread that runs the scheduled task at the specified time. 

If your Timer thread is not daemon then it will stop your application from exits until it completes all scheduled tasks. It's recommended that TimerTask should not be very long otherwise it can keep this thread busy and not allow other scheduled tasks to get completed. 

This can delay the execution of other scheduled tasks, which may queue up and execute in quick succession once the offending task is completed.

Here is a nice diagram which illustrate how Timer works in Java:





Difference between Timer and TimerTask in Java

I have seen programmers getting confused between Timer and TimerTask, which is quite unnecessary because these two are altogether different. You just need to remember:

1) Timer in Java schedules and execute TimerTask which is an implementation of Runnable interface and overrides run method to define the actual task performed by that TimerTask.

2) Both Timer and TimerTask provide a cancel() method. Timer's cancel() method cancels the whole timer while TimerTask's one cancels only a particular task. I think this is the worth noting difference between Timer and TimerTask in Java.

Also, here is a nice table which highlights all the difference between Timer and TimerTask in Java:




Canceling Timer in Java

You can cancel Java Timer by calling the cancel() method of java.util.Timer class, this would result in the following:
1) Timer will not cancel any currently executing task.
2) Timer will discard other scheduled tasks and will not execute them.
3) Once the currently executing task will be finished, the timer thread will terminate gracefully.
4) Calling Timer.cancel() more than one time will not affect. the second call will be ignored.

In addition to canceling Timer, You can also cancel individual TimerTask by using the cancel() method of TimerTask itself. You can further see these Java Multithreading courses to learn more about Thread and TimerTask in Java. 




Timer and TimerTask example to schedule Tasks

Here is one example of Timer and TimerTask in Java to implement Reminder utility.

public class JavaReminder {
    Timer timer;

    public JavaReminder(int seconds) {
        timer = new Timer();  //At this line a new Thread will be created
        timer.schedule(new RemindTask(), seconds*1000); //delay in milliseconds
    }

    class RemindTask extends TimerTask {

        @Override
        public void run() {
            System.out.println("ReminderTask is completed by Java timer");
            timer.cancel(); //Not necessary because we call System.exit
            //System.exit(0); //Stops the AWT thread (and everything else)
        }
    }

    public static void main(String args[]) {
        System.out.println("Java timer is about to start");
        JavaReminder reminderBeep = new JavaReminder(5);
        System.out.println("Remindertask is scheduled with Java timer.");
    }
}

Output
Java timer is about to start
Remindertask is scheduled with a Java timer.
ReminderTask is completed by Java timer  //this will print after 5 seconds


Important points on Timer and TimerTask in Java

Timer and TimerTask example in JavaNow we know what is Timer and TimerTask in Java, How to use them, How to cancel then and got an understanding of How Timer works in Java. It’s a good time to revise the Timer and TimerTask.

1. One Thread will be created corresponding ot each Timer in Java, which could be either daemon or user thread.
2.You can schedule multiple TimerTask with one Timer.
3.You can schedule tasks for either one-time execution or recurring execution.
4.TimerTask.cancel() cancels only that particular task, while Timer.cancel() cancel all task scheduled in Timer.
5. Timer in Java will throw IllegalStateException if you try to schedule a task on a Timer that has been canceled or whose Task execution Thread has been terminated.



That's all on what is Timer and TimerTask in Java and the difference between Timer and TimerTask in Java. A good understanding of Timer API is required by Java programmers to take maximum advantage of scheduling features provided by Timer. They are essential and can be used in a variety of ways e.g. to periodically remove clean cache,  to perform timely jobs, etc.


Other Java Multithreading Tutorials from Javarevisited Blog

11 comments :

garima said...

Hi Javin,
The program which you have provided here is for one time execution.
What are changes need to be done in program for recurring execution?

Jiri Pinkas said...

Timer is great. I used it a lot until I found this class:

http://docs.oracle.com/javase/6/docs/api/java/util/concurrent/ScheduledThreadPoolExecutor.html

From API: "This class is preferable to Timer when multiple worker threads are needed, or when the additional flexibility or capabilities of ThreadPoolExecutor (which this class extends) are required."

Javin @ ClassLoader in Java said...

@garima, You just need to use different schedule() method from Timer class. For recurring execution you can following schedule() method :

public void schedule(TimerTask task,
long delay,
long period)

This will schedule task for repeated execution, first execution will be after delay specified by second argument and than subsequent recurring execution will be separated by period, third argument.

Javin @ xml interview questions said...

@Jiri Pinkas, Indeed ScheduledThreadPoolExecutor is good to know and as you said has a clear advantage when you need multiple worker thread.

Anonymous said...

Hi , Facing problem while using timers. I have scheduled job like the below. But its not triggerng when the time comes.

for(i=0;i<=3;i++)
{
Timer timer = new Timer()
timer.schedule(new Timertask() {
pubil void run()
{
System.out.println("triggereed");
}
},timevalue);
}

For the first time its getting triggered and for the next two times its not printing. and no erro is also thrown.

Is there a way to check the scheudle jobs list using the timer instance or any other way we could debug this.

Please help me

Anonymous said...

Hi Javin,
Nice Explanation, but I have question, how can it knows that the task is complete or not?

Anonymous said...

...As said, it is useful for short tasks and it's effective for more purpose but the presence of classic synchronization can be a problem in some stress condition. java.util.Timer it's also used in some old JBoss Server or JDBC Driver. It useful to have some standard classes (TimeTask is an Abstract Class to Extends) that define common Task and inside we can put simple logic to know what goes on and when (maybe we will find that over some level it's better another approach)
We need to put attention to have short task, when we use it, we need to cancel unused tasks, we need to purge cancelled tasks. Sometimes, we need to force some clean, playing on internal field with reflection: for example, knowning TimeThread, you can clean and shutdown a leaked Timer...

try {
final Field newTasksMayBeScheduled = ReflectionUtil.findField(thread.getClass(),"newTasksMayBeScheduled");
final Object queue = ReflectionUtil.findField(thread.getClass(), "queue").get(thread);
final Method clear = ReflectionUtil.getDeclaredMethod(queue,"clear");
clear.setAccessible(true);
synchronized (queue) {
newTasksMayBeScheduled.set(thread,false);
clear.invoke(queue);
queue.notify(); }}catch (Exception ex) {...}

javin paul said...

@Anonymous 1, There is no way to check if a Task is completed or not. You need to put your logic there e.g. when run() method is successfully executed you can set a boolean to indicate that job is successful. You can set it false in case of any exception.

javin paul said...

@Anonymous, Yes, cleanup is responsibility of developer here. I have seen cases of memory leak with hung TimerTask holding references of big objects.

Anonymous said...

Another advantage of Timer class is that it is threadsafe also.

HARYOURBARMIEY said...

package com.blundell.tut.ui.phone;

import java.util.Calendar;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.DatePicker;
import android.widget.Toast;

import com.blundell.tut.R;
import com.blundell.tut.service.ScheduleClient;

/**
* This is the Main Activity of our app.
* Here we allow the user to select a date,
* we then set a notification for that date to appear in the status bar
*
* @author HARYOURBARMIEY
*/
public class MainActivity extends Activity {
// This is a handle so that we can call methods on our service
private ScheduleClient scheduleClient;
// This is the date picker used to select the date for our notification
private DatePicker picker;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// Create a new service client and bind our activity to this service
scheduleClient = new ScheduleClient(this);
scheduleClient.doBindService();

// Get a reference to our date picker
picker = (DatePicker) findViewById(R.id.scheduleTimePicker);
}

/**
* This is the onClick called from the XML to set a new notification
*/
public void onDateSelectedButtonClick(View v){
// Get the date from our datepicker
int day = picker.getDayOfMonth();
int month = picker.getMonth();
int year = picker.getYear();
// Create a new calendar set to the date chosen
// we set the time to midnight (i.e. the first minute of that day)
Calendar c = Calendar.getInstance();
c.set(year, month, day);
c.set(Calendar.HOUR_OF_DAY, 0);
c.set(Calendar.MINUTE, 0);
c.set(Calendar.SECOND, 0);
// Ask our service to set an alarm for that date, this activity talks to the client that talks to the service
scheduleClient.setAlarmForNotification(c);
// Notify the user what they just did
Toast.makeText(this, "Notification set for: "+ day +"/"+ (month+1) +"/"+ year, Toast.LENGTH_SHORT).show();
}

@Override
protected void onStop() {
// When our activity is stopped ensure we also stop the connection to the service
// this stops us leaking our activity into the system *bad*
if(scheduleClient != null)
scheduleClient.doUnbindService();
super.onStop();
}
}

Post a Comment