Java Enterprise Java Class of the Month: Control Event Executions with the New Timer Class

Class of the Month: Control Event Executions with the New Timer Class

Amidst all the changes and additions in JDK 1.3, was a
small but useful class called Timer. Timer and TimerTask both belong to the java.util
package. They facilitate execution of code at a predetermined time or during specified
time intervals. In Unix, you can use the “at” command to schedule a process to run at
specific times. A similar facility exists under NT servers. There are, however, times
when you need to schedule execution of a particular task within a Java program.




Piroz Mohseni

Let’s look at a very simple example.

The class ttask inherits from
TimerTask and implements a single run()
method. Similar to threads, the code inside of the run() method is what
is executed on a scheduled basis. In our case, we simply print
out a message. Another method that you can implement is cancel(),
which stops the task. You only need to call this method once, because
after an event is cancelled, it doesn’t make sense to cancel it again. You won’t get
an error, however, if you repeatedly call cancel. It just won’t do anything.

Finally, the last method of TimerTask is scheduledExecutionTime().
It returns the scheduled execution time of the task. You can use this value to
decide whether you should skip execution or take other actions.

If your application requires scheduling and fixed-time
execution of
tasks, you should take a look at Timer and TimerTask classes.

We can now use the class tttask. First, we instantiate a new Timer. We then call
the scheduleAtFixedRate() method. This
method will take three arguments. The first is the instance of TimerTask that we
want to execute. The second argument is either a Date or the number of milliseconds
specifying a delay before execution takes place. The third argument is the number
of milliseconds that should elapse between each interval. Using this method, you
can schedule a certain task to execute at a prespecified date and time in the future
and repeat itself at a fixed interval.


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

public class test {
public static void main(final String args[]) {

final Timer timer = new Timer();
timer.scheduleAtFixedRate(new ttask(), 5000, 1000);

}

}

class ttask extends TimerTask {
public void run() {
System.out.println(“hello”);
}
}

The other way to use Timer is to call its schedule()
method. The simplest form takes a TimerTask instance and a
Date as its argument. This is useful where you just want to schedule the task for a
single execution.

There are other variations, as well, where you can specify a
fixed interval or a delay parameter. The class is thread-safe, allowing a single
instance to support multiple threads. A note of caution is that the timing parameters
are not absolute. While the VM will do its best to honor the timing parameters,
there are situations, such as garbage collection, where the specified fixed times will
become approximates.

If your application requires scheduling and fixed-time
execution of tasks, you should take a look at Timer and TimerTask classes.

About the Author

Piroz Mohseni
is president of Bita Technologies, focusing on business improvement through the
effective use of technology. His areas of interest include enterprise Java, XML,
and e-commerce applications.

Latest Posts

Related Stories