@ThreadSafe public final class SwingTimerTimingSource extends TimingSource
Timer
and the
SwingUtilities.invokeLater(Runnable)
method. This implementation
ensures that calls to registered TickListener
and
PostTickListener
objects are always made within the thread context of
the Swing EDT.
A typical use, where tl
is a TickListener
object, would be
TimingSource ts = new SwingTimerTimingSource(15, TimeUnit.MILLISECONDS); ts.init(); // starts the timer ts.addTickListener(tl); // tl gets tick notifications in the Swing EDT ts.removeTickListener(tl); // tl stops getting notifications ts.dispose(); // done using tsIf you are not sure what period to set, use the
SwingTimerTimingSource()
constructor which uses a reasonable default
value of 15 milliseconds.
Tasks submitted to TimingSource.submit(Runnable)
and calls to registered
TickListener
and PostTickListener
objects from this timing
source are always made in the context of the Swing EDT.
TimingSource.PostTickListener, TimingSource.TickListener
Constructor and Description |
---|
SwingTimerTimingSource()
Constructs a new instance with a period of 15 milliseconds.
|
SwingTimerTimingSource(long period,
java.util.concurrent.TimeUnit unit)
Constructs a new instance.
|
Modifier and Type | Method and Description |
---|---|
void |
dispose()
Stops the timing source and disposes of its resources.
|
void |
init()
Starts up the timing source.
|
boolean |
isDisposed()
Gets if this timing target has had
TimingSource.dispose() invoked on it. |
java.lang.String |
toString() |
addPostTickListener, addTickListener, removePostTickListener, removeTickListener, runPerTick, submit
public SwingTimerTimingSource(long period, java.util.concurrent.TimeUnit unit)
init()
must be called on the new
instance to start the timer. The dispose()
method should be called
to stop the timer.
The Swing timer requires a period of at least 1 millisecond. If the period
passed is smaller, it is set to 1 millisecond. It is also not recommended
to pass a period much larger than a day as this could lead to integer
overflow in the case that the value in milliseconds cannot be represented
as an int to be passed to the Swing Timer
.
period
- the period of time between "tick" events.unit
- the time unit of period parameter.public SwingTimerTimingSource()
public void init()
TimingSource
init
in class TimingSource
public void dispose()
TimingSource
dispose
in class TimingSource
public boolean isDisposed()
TimingSource
TimingSource.dispose()
invoked on it.isDisposed
in class TimingSource
true
if this timing target has had TimingSource.dispose()
invoked on it, false
otherwise.public java.lang.String toString()
toString
in class java.lang.Object