THE Java™ Programming Language, Fourth Edition

(Jeff_L) #1
public longscheduledExecutionTime()

Returns the scheduled execution time of the most recent actual execution
(possibly the in-progress execution) of this TimerTask. The returned value
represents the time in milliseconds. This method is most often used inside
run to see if the current execution of the task occurred soon enough to
warrant execution; if the task was delayed too long run may decide not to do
anything.

You can cancel either a single task or an entire timer by invoking the cancel method of TimerTask or
Timer. Cancelling a task means that it will not be scheduled in the future. Cancelling a Timer object
prevents any future execution of any of its tasks. If you purge a timer then all references to cancelled tasks
are removed from its internal queue of tasks to schedule.


Each Timer object uses a single thread to schedule its task executions. You can control whether this thread is
a daemon thread by the boolean you specify to the Timer constructor.


publicTimer(boolean isDaemon)

Creates a new Timer whose underlying thread has its daemon state set
according to isDaemon.

publicTimer()

Equivalent to Timer(false).

publicTimer(String name)

Equivalent to Timer(false) but gives the associated thread the given
name. This is useful for debugging and monitoring purposes.

publicTimer(String name, boolean isdaemon)

Equivalent to Timer(isDaemon) but gives the associated thread the given
name. This is useful for debugging and monitoring purposes.

If the thread is not specified to be a daemon, it will be a user thread. When the timer is garbage
collectedwhich can only happen when all references to it are dropped and no tasks remain to be executedthe
user thread will terminate. You should not rely on this behavior since it depends 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 execution to be
similarly delayed, so that an execution starts relative to the start of the previous execution. In contrast, a
fixed-rate scheduling 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 voidschedule(TimerTask task, Date time)
Free download pdf