@ThreadSafe public final class ScheduledExecutorTimingSource extends TimingSource
ScheduledExecutorService
as returned from
Executors.newSingleThreadScheduledExecutor()
.
A typical use, where tl
is a TickListener
object, would be
TimingSource ts = new ScheduledExecutorTimingSource(15, TimeUnit.MILLISECONDS); ts.init(); // starts the timer ts.addTickListener(tl); // tl gets tick notifications ts.removeTickListener(tl); // tl stops getting notifications ts.dispose(); // done using tsIf you are not sure what period to set, use the
ScheduledExecutorTimingSource()
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 a single thread. This thread is the
thread created by Executors.newSingleThreadScheduledExecutor()
.
TimingSource.PostTickListener, TimingSource.TickListener
Constructor and Description |
---|
ScheduledExecutorTimingSource()
Constructs a new instance with a period of 15 milliseconds.
|
ScheduledExecutorTimingSource(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 ScheduledExecutorTimingSource(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.period
- the period of time between "tick" events.unit
- the time unit of period parameter.public ScheduledExecutorTimingSource()
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