Using the Timer and TimerTask Classes (2025)

The JavaTM Tutorial
Start of Tutorial>Start of Trail>Start of LessonSearch
Feedback Form
Using the Timer and TimerTask Classes (4)
Trail: Essential Java Classes
Lesson: Threads: Doing Two or More Tasks At Once
This section discusses practical aspects of using timers to schedule tasks. TheTimerUsing the Timer and TimerTask Classes (5) class in the java.util package schedules instances of a class calledTimerTaskUsing the Timer and TimerTask Classes (6).Reminder.java is an example of using a timer to perform a task after a delay:
import java.util.Timer;import java.util.TimerTask;/** * Simple demo that uses java.util.Timer to schedule a task * to execute once 5 seconds have passed. */public class Reminder { Timer timer; public Reminder(int seconds) { timer = new Timer(); timer.schedule(new RemindTask(), seconds*1000);} class RemindTask extends TimerTask { public void run() { System.out.println("Time's up!"); timer.cancel(); //Terminate the timer thread } } public static void main(String args[]) { new Reminder(5); System.out.println("Task scheduled."); }}

When you run the example, you first see this:

Task scheduled.

Five seconds later, you see this:

Time's up!

This simple program illustrates the basic parts ofimplementing and scheduling a task to be executed by a timer thread.

  • Implement a custom subclass of TimerTask. The run method contains the code that performs the task. In this example, the subclass is named RemindTask.
  • Create a thread by instantiating the Timer class.
  • Instantiate the timer task object (new RemindTask()).
  • Schedule the timer task for execution. This example uses the schedule method, with the timer task as the first argument and the delay in milliseconds (5000) as the second argument. Another way of scheduling a task is to specify the time when the task should execute. For example, the following code schedules a task for execution at 11:01 p.m.:
    //Get the Date corresponding to 11:01:00 pm today.Calendar calendar = Calendar.getInstance();calendar.set(Calendar.HOUR_OF_DAY, 23);calendar.set(Calendar.MINUTE, 1);calendar.set(Calendar.SECOND, 0);Date time = calendar.getTime();timer = new Timer();timer.schedule(new RemindTask(), time);

Stopping Timer Threads

By default, a program keeps running as long as itstimer threads are running.You can terminate a timer thread in four ways.
  • Invoke cancel on the timer. You can do this from anywhere in the program, such as from a timer task's run method.
  • Make the timer's thread a "daemon" by creating the timer like this: new Timer(true). If the only threads left in the program are daemon threads, the program exits.
  • After all the timer's scheduled tasks have finished executing, remove all references to the Timer object. Eventually, the timer's thread will terminate.
  • Invoke the System.exit method, which makes the entire program (and all its threads) exit.
The Reminder example uses the first scheme,invoking the cancel method from the timer task's run method. Making the timer thread a daemon wouldn'twork, because the program needs to keep running until the timer's taskexecutes.

Sometimes, timer threads aren't the only threadsthat can prevent a program from exiting when expected.For example, if you use the AWT at all — even ifonly to make beeps — the AWT automatically creates a nondaemon thread that keeps the program alive.The following modification of Reminder adds beeping,which requires us to also add a call tothe System.exit method to make the program exit.Significant changes are in highlighted.You can find the source code inReminderBeep.java.

public class ReminderBeep { ... public ReminderBeep(int seconds) { toolkit = Toolkit.getDefaultToolkit(); timer = new Timer(); timer.schedule(new RemindTask(), seconds*1000); } class RemindTask extends TimerTask { public void run() { System.out.println("Time's up!"); toolkit.beep(); //timer.cancel(); // Not necessary because // we call System.exit System.exit(0); // Stops the AWT thread // (and everything else) } } ...}

Performing a Task Repeatedly

Here’s an example of using a timer to perform a task once per second.
public class AnnoyingBeep { Toolkit toolkit; Timer timer; public AnnoyingBeep() { toolkit = Toolkit.getDefaultToolkit(); timer = new Timer(); timer.schedule(new RemindTask(), 0, //initial delay 1*1000); //subsequent rate } class RemindTask extends TimerTask { int numWarningBeeps = 3; public void run() { if (numWarningBeeps > 0) { toolkit.beep(); System.out.println("Beep!"); numWarningBeeps--; } else { toolkit.beep(); System.out.println("Time's up!"); //timer.cancel(); // Not necessary because // we call System.exit System.exit(0); // Stops the AWT thread // (and everything else) } } } ...}
You can find the entire program in AnnoyingBeep.java. When you execute it,you see the following output(our comments about timing are shown in italics):
Task scheduled.Beep! Beep! //one second after the first beepBeep! //one second after the second beepTime's up! //one second after the third beep
The AnnoyingBeep program usesa three-argument version of the schedule method to specifythat its task should execute once a second, beginning immediately.Here are all the Timermethods you can use to schedule repeated executions of tasks:
  • schedule(TimerTask task, long delay, long period)
  • schedule(TimerTask task, Date time, long period)
  • scheduleAtFixedRate(TimerTask task, long delay, long period)
  • scheduleAtFixedRate(TimerTask task, Date firstTime, long period)

When scheduling a task for repeated execution, you should use one of theschedule methods when smoothness is important and ascheduleAtFixedRate methodwhen time synchronization is more important.For example, the AnnoyingBeep programuses the schedule method,which means that the annoying beeps will all be at least 1 second apart.If one beep is late for any reason,all subsequent beeps will be delayed.If we decide that the AnnoyingBeep programshould exit exactly 3 seconds after the first beep — evenif it means that two beeps might occur closetogether if a beep is delayed for any reason — weshould use the scheduleAtFixedRate method instead.

More Information about Timers

The timer tasks we've shown have been very simple.They do almost nothing and refer only to data that either can besafely accessed from multiple threads or is private to the timer task.As long as your timer task uses only API designed to bethread-safe — such as the methods in the Timer class — implementing timers is relatively straightforward.However, if your timer implementation depends on shared resources,such as data used by other places in your program,you need to be careful.You can find out more later in this chapter in the sectionSynchronizing Threads.

Using the Timer and TimerTask Classes (11)
Start of Tutorial>Start of Trail>Start of LessonSearch
Feedback Form

Copyright1995-2005 Sun Microsystems, Inc. All rights reserved.

Using the Timer and TimerTask Classes (2025)
Top Articles
Latest Posts
Recommended Articles
Article information

Author: Rob Wisoky

Last Updated:

Views: 5560

Rating: 4.8 / 5 (48 voted)

Reviews: 95% of readers found this page helpful

Author information

Name: Rob Wisoky

Birthday: 1994-09-30

Address: 5789 Michel Vista, West Domenic, OR 80464-9452

Phone: +97313824072371

Job: Education Orchestrator

Hobby: Lockpicking, Crocheting, Baton twirling, Video gaming, Jogging, Whittling, Model building

Introduction: My name is Rob Wisoky, I am a smiling, helpful, encouraging, zealous, energetic, faithful, fantastic person who loves writing and wants to share my knowledge and understanding with you.