Java Reference
In-Depth Information
pends on the garbage collector discovering the unreachability of the
timer object, which can happen anytime or never.
There are three kinds of task scheduling. A
once-only
scheduling means
that the task will be executed once. A
fixed-delay
scheduling lets you
define the amount of time between the start of one execution and the
next. The task essentially executes periodically, but any delay in the
start of one execution (due to general thread scheduling considerations,
garbage collection, or other background activity) causes the next ex-
ecution to be similarly delayed, so that an execution starts relative to
the start of the previous execution. In contrast, a
fixed-rate
schedul-
ing starts each execution relative to the start of the initial execution. If
one execution is delayed, there will be a shorter gap before the next
executionpossibly no gap, depending on the extent of the delay. You
use fixed-delay scheduling when the frequency of tasks is what matters,
such as time delays between animation frames in some circumstances.
You use fixed-rate scheduling when the absolute time matters, such as
alarms or timers.
public void
schedule(TimerTask task, Date time)
Schedules the given
task
for a once-only execution at the spe-
cified
time
.
public void
schedule(TimerTask task, long delay)
Schedules the given
task
for a once-only execution after the
specified
delay
(in milliseconds),
public void
schedule(TimerTask task, Date firstTime, long period)
Schedules the given
task
to execute on a fixed-delay schedule
until cancelled, starting at
firstTime
, executing every
period
milliseconds.
public void
schedule(TimerTask task, long delay, long period)