Java Reference
In-Depth Information
Table 2-1. Timer Properties
Property Name
Data Type
Access
actionListeners
ActionListener[ ]
Read-only
coalesce
boolean
Read-write
delay
int
Read-write
initialDelay
int
Read-write
repeats
boolean
Read-write
running
boolean
Read-only
The delay property is the same as the constructor argument. If you change the delay of a
running timer, the new delay won't be used until the existing delay runs out.
The initialDelay property allows you to have another startup delay besides the periodic
delay after the first execution. For instance, if you don't want to initially do a task for an hour,
but then want to do it every 15 minutes thereafter, you need to change the initialDelay setting
before you start the timer. By default, the initialDelay and delay properties are set to the same
setting in the constructor.
The repeats property is true by default, which results in a repeating timer. When false ,
the timer notifies action listeners only once. You then need to restart() the timer to trigger the
listener again. Nonrepeating timers are good for onetime notifications that need to happen
after a triggering event.
The coalesce property allows for a busy system to throw away notifications that haven't
happened yet when a new event needs to be fired to the registered ActionListener objects. By
default, the coalesce value is true . This means if a timer runs every 500 milliseconds, but its
system is bogged down and doesn't respond for a whole 2 seconds, the timer needs to send
only one message, rather than also sending the missing ones. If the setting were false , four
messages would still need to be sent.
In addition to the properties just listed, you can turn on log messages with the following
line of code:
Timer.setLogTimers(true);
Log messages are good for actions that lack a visual element, allowing you to see when
something happens.
Tip The java.util.Timer class works in a fashion similar to the javax.swing.Timer class, except that
it doesn't run the scheduled task in the event-dispatch thread. In addition, it supports executing tasks at a
fixed rate, versus after a fixed delay. The latter scheme permits the repeat rate to drift between executions if
the event-dispatch thread is busy.
 
Search WWH ::




Custom Search