Name
Timer
Synopsis
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 ...
Get Java in a Nutshell, 5th Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.