@ThreadSafe public abstract class TimingSource extends java.lang.Object
A timing source needs to "tick" at some regular period of time. This period, if it is variable for a particular implementation, should be specified to the subclass's constructor.
A timer notifies a registered set of TimingSource.TickListener
s that a tick of
time has elapsed. In addition, a timer notifies a set of
TimingSource.PostTickListener
s after the registered set of TimingSource.TickListener
s
has been notified. For example, a TimingSource.PostTickListener
could be used to
call repaint() after a large number of animations (which register
themselves as TimingSource.TickListener
s) have updated the program's state. One
time tasks can be queued to be run at the next tick of time (before any other
listeners are called) using submit(Runnable)
.
A timer should begin ticking after init()
is called and should be
stopped and disposed after dispose()
is called. The timer cannot be
restarted after dispose()
is called.
A timing source implementation should document the thread context in which it
makes calls to registered listeners. Further, it should invoke
runPerTick()
at each tick of time to perform the calls to registered
listeners.
Modifier and Type | Class and Description |
---|---|
static interface |
TimingSource.PostTickListener
This interface is implemented by any object wishing to receive "tick"
events from a
TimingSource object after all registered
TimingSource.TickListener objects have been notified. |
static interface |
TimingSource.TickListener
This interface is implemented by any object wishing to receive "tick"
events from a
TimingSource object. |
Constructor and Description |
---|
TimingSource() |
Modifier and Type | Method and Description |
---|---|
void |
addPostTickListener(TimingSource.PostTickListener listener)
Adds a
TimingSource.PostTickListener to the set of listeners that receive timing
events from this TimingSource . |
void |
addTickListener(TimingSource.TickListener listener)
Adds a
TimingSource.TickListener to the set of listeners that receive timing
events from this TimingSource . |
abstract void |
dispose()
Stops the timing source and disposes of its resources.
|
abstract void |
init()
Starts up the timing source.
|
abstract boolean |
isDisposed()
Gets if this timing target has had
dispose() invoked on it. |
void |
removePostTickListener(TimingSource.PostTickListener listener)
Removes a
TimingSource.PostTickListener from the set of listeners that receive
timing events from this TimingSource . |
void |
removeTickListener(TimingSource.TickListener listener)
Removes a
TimingSource.TickListener from the set of listeners that receive
timing events from this TimingSource . |
void |
runPerTick()
Used by timing source implementations to perform the following actions in
the listed order:
Execute all queued "one shot" tasks in the order they were received.
Notify all registered
TimingSource.TickListener s
Notify all registered TimingSource.PostTickListener s
A typical implementation will invoke this method when its particular timer
calls back each tick of time. |
void |
submit(java.lang.Runnable task)
Runs the passed task in the thread context of this timing source.
|
public abstract void init()
public abstract void dispose()
public abstract boolean isDisposed()
dispose()
invoked on it.true
if this timing target has had dispose()
invoked on it, false
otherwise.public final void addTickListener(TimingSource.TickListener listener)
TimingSource.TickListener
to the set of listeners that receive timing
events from this TimingSource
. Has no effect if the listener has
already been added.listener
- the listener to be added.public final void removeTickListener(TimingSource.TickListener listener)
TimingSource.TickListener
from the set of listeners that receive
timing events from this TimingSource
. Has no effect if the listener
is not in the set of listeners.listener
- the listener to be removed.public final void addPostTickListener(TimingSource.PostTickListener listener)
TimingSource.PostTickListener
to the set of listeners that receive timing
events from this TimingSource
. Has no effect if the listener has
already been added.listener
- the listener to be added.public final void removePostTickListener(TimingSource.PostTickListener listener)
TimingSource.PostTickListener
from the set of listeners that receive
timing events from this TimingSource
. Has no effect if the listener
is not in the set of listeners.listener
- the listener to be removed.public final void submit(java.lang.Runnable task)
TimingSource.TickListener
s). In particular, this method will
not block for execution of the task and the task will not execute until
after init()
has not been called. Tasks are executed in the order
they are received.
The task is wrapped, via WrappedRunnable
, to log an error if it
fails due to an unhandled exception.
This method is used to execute a snippet of code in the thread context used
for TimingSource.TickListener
s and TimingSource.PostTickListener
s.
task
- a task.WrappedRunnable
public void runPerTick()
TimingSource.TickListener
sTimingSource.PostTickListener
sThis method should never be called by client code—it is only intended to be used by timing source implementations. It is declared public only to avoid the generation of a synthetic accessor method for it, because many implementations invoke this method within nested classes.