This class
implements a timer: its methods allow you to schedule one or more
runnable TimerTask objects to be executed (once or
repetitively) by a background thread at a specified time in the
future. You can create a timer with the Timer( )
constructor. The no-argument version of this constructor creates a
regular non-daemon background thread, which means that the Java VM
will not terminate while the timer thread is running. Pass
true to the constructor if you want the background
thread to be a daemon thread. In Java 5.0 you can also specify the
name of the background thread when creating a
Timer.
Once you have created a
Timer, you can schedule
TimerTask objects to be run in the future with the
various schedule( ) and
scheduleAtFixedRate( ) methods. To schedule a task
for a single execution, use one of the two-argument
schedule( ) methods and specify the desired
execution time either as a number of milliseconds in the future or as
an absolute Date. If the number of milliseconds is
0, or if the Date object
represents a time already passed, the task is scheduled for immediate
execution.
To schedule a repeating task, use one of the three-argument versions
of schedule( ) or scheduleAtFixedRate(
). These methods are passed an argument that specifies the
time (either as a number of milliseconds or as a
Date object) of the first execution of the task
and another argument, period, that
specifies the number of milliseconds between repeated executions of
the task. The schedule( ) methods schedule the
task for fixed-interval execution. That is, each
execution is scheduled for period
milliseconds after the previous execution ends.
Use schedule( ) for tasks such as animation, where
it is important to have a relatively constant interval between
executions. The scheduleAtFixedRate( ) methods, on
the other hand, schedule tasks for fixed-rate
execution. That is, each repetition of the task is scheduled for
period milliseconds after the previous
execution begins. Use
scheduleAtFixedRate( ) for tasks, such as updating
a clock display, that must occur at specific absolute times rather
than at fixed intervals.
A single Timer object can comfortably schedule
many TimerTask objects. Note, however, that all
tasks scheduled by a single Timer share a single
thread. If you are scheduling many rapidly repeating tasks, or if
some tasks take a long time to execute, other tasks may have their
scheduled executions delayed.
When you are done with a
Timer, call cancel( ) to stop
its associated thread from running. This is particularly important
when you are using a timer whose associated thread is not a daemon
thread, because otherwise the timer thread can prevent the Java VM
from exiting. To cancel the execution of a particular task, use the
cancel( ) method of TimerTask.
public class Timer {
// Public Constructors
public Timer( );
public Timer(boolean isDaemon);
5.0 public Timer(String name);
5.0 public Timer(String name, boolean isDaemon);
// Public Instance Methods
public void cancel( );
5.0 public int purge( );
public void schedule(TimerTask task, long delay);
public void schedule(TimerTask task, Date time);
public void schedule(TimerTask task, long delay, long period);
public void schedule(TimerTask task, Date firstTime, long period);
public void scheduleAtFixedRate(TimerTask task, long delay, long period);
public void scheduleAtFixedRate(TimerTask task, Date firstTime, long period);
}