- java.lang.Object
-
- swim.concurrent.Clock
-
- All Implemented Interfaces:
Schedule
- Direct Known Subclasses:
StageClock
public class Clock extends Object implements Schedule
Hashed wheel timerSchedule
.
-
-
Field Summary
Fields Modifier and Type Field Description static int
TICK_COUNT
Default number of ticks per clock revolution, used by the no-argClock()
constructor.static int
TICK_MILLIS
Default number of milliseconds between clock ticks, used by the no-argClock()
constructor.
-
Constructor Summary
Constructors Constructor Description Clock()
Constructs a newClock
with a timer resolution ofTICK_MILLIS
milliseconds, and a clock period ofTICK_COUNT
ticks per revolution.Clock(int tickMillis, int tickCount)
Constructs a newClock
with a timer resolution oftickMillis
milliseconds, and a clock period oftickCount
ticks per revolution.Clock(ClockDef clockDef)
Constructs a newClock
with the timer resolution and clock period specified by the givenclockDef
.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected void
didFail(Throwable error)
Lifecycle callback invoked if the timer thread throws a fatalerror
.protected void
didStart()
Lifecycle callback invoked after the clock thread starts.protected void
didStop()
Lifecycle callback invoked after the clock thread stops.protected void
didTick(long tick, long waitedMillis)
Introspection callback invoked after each tick of the clock.protected long
nanoTime()
Returns the current time, in nanoseconds, with arbitrary origin.protected void
runTimer(TimerFunction timer, Runnable runnable)
Invokestimer.runTimer()
, or arranges for the asynchronous execution of the providedrunnable
, which will itself invoketimer.runTimer()
.TimerRef
setTimer(long millis, TimerFunction timer)
Schedulestimer
to execute aftermillis
milliseconds have elapsed.protected void
sleep(long millis)
Parks the current thread for the specified number ofmillis
.void
start()
Ensures that thisClock
is up and running, starting up the clock thread if it has not yet been started.void
stop()
Ensures that thisClock
has been permanently stopped, shutting down the clock thread, if it's currently running.long
tick()
Returns the tick sequence number of the lowest clock tick that has yet to finish executing.TimerRef
timer(TimerFunction timer)
Returns an unscheduledTimerRef
bound totimer
, which can later be used to scheduletimer
.protected void
timerDidCancel(TimerFunction timer)
Introspection callback invoked after atimer
has been explicitly cancelled; not invoked when a timer is implicitly cancelled, such as when rescheduling an already scheduled timer.protected void
timerDidFail(TimerFunction timer, Throwable error)
Introspection callback invoked after atimer
execution fails by throwing anerror
.protected void
timerDidRun(TimerFunction timer)
Introspection callback invoked after atimer
executes nominally.protected void
timerWillRun(TimerFunction timer)
Introspection callback invoked before atimer
is executed.protected void
timerWillSchedule(TimerFunction timer, long millis)
Introspection callback invoked before atimer
is scheduled for execution with a delay ofmillis
milliseconds.protected void
willStart()
Lifecycle callback invoked before the clock thread starts.protected void
willStop()
Lifecycle callback invoked before the clock thread stops.
-
-
-
Field Detail
-
TICK_MILLIS
public static final int TICK_MILLIS
Default number of milliseconds between clock ticks, used by the no-argClock()
constructor. Defaults to the value of theswim.clock.tick.millis
system property, if defined; otherwise defaults to100
milliseconds.
-
TICK_COUNT
public static final int TICK_COUNT
Default number of ticks per clock revolution, used by the no-argClock()
constructor. Defaults to the value of theswim.clock.tick.count
system property, if defined; otherwise defaults to512
clock ticks per revolution.
-
-
Constructor Detail
-
Clock
public Clock(int tickMillis, int tickCount)
Constructs a newClock
with a timer resolution oftickMillis
milliseconds, and a clock period oftickCount
ticks per revolution.
-
Clock
public Clock(ClockDef clockDef)
Constructs a newClock
with the timer resolution and clock period specified by the givenclockDef
.
-
Clock
public Clock()
Constructs a newClock
with a timer resolution ofTICK_MILLIS
milliseconds, and a clock period ofTICK_COUNT
ticks per revolution.
-
-
Method Detail
-
tick
public final long tick()
Returns the tick sequence number of the lowest clock tick that has yet to finish executing.
-
start
public final void start()
Ensures that thisClock
is up and running, starting up the clock thread if it has not yet been started.- Throws:
ScheduleException
- if thisClock
has been stopped.
-
stop
public final void stop()
Ensures that thisClock
has been permanently stopped, shutting down the clock thread, if it's currently running. Upon return, thisClock
is guaranteed to be in the stopped state.
-
timer
public TimerRef timer(TimerFunction timer)
Description copied from interface:Schedule
Returns an unscheduledTimerRef
bound totimer
, which can later be used to scheduletimer
.
-
setTimer
public TimerRef setTimer(long millis, TimerFunction timer)
Description copied from interface:Schedule
Schedulestimer
to execute aftermillis
milliseconds have elapsed. Returns aTimerRef
that can be used to check the status of, reschedule, and canceltimer
.
-
willStart
protected void willStart()
Lifecycle callback invoked before the clock thread starts.
-
didStart
protected void didStart()
Lifecycle callback invoked after the clock thread starts.
-
didTick
protected void didTick(long tick, long waitedMillis)
Introspection callback invoked after each tick of the clock.tick
is the sequence number of the tick that was executed;waitedMillis
is the number of milliseconds the clock thread slept before executing the tick. IfwaitedMillis
is negative, then the clock thread didn't start executing the tick until-waitedMillis
milliseconds after the scheduled tick deadline.
-
willStop
protected void willStop()
Lifecycle callback invoked before the clock thread stops.
-
didStop
protected void didStop()
Lifecycle callback invoked after the clock thread stops.
-
didFail
protected void didFail(Throwable error)
Lifecycle callback invoked if the timer thread throws a fatalerror
. The clock thread will stop after invokingdidFail
.
-
timerWillSchedule
protected void timerWillSchedule(TimerFunction timer, long millis)
Introspection callback invoked before atimer
is scheduled for execution with a delay ofmillis
milliseconds.
-
timerDidCancel
protected void timerDidCancel(TimerFunction timer)
Introspection callback invoked after atimer
has been explicitly cancelled; not invoked when a timer is implicitly cancelled, such as when rescheduling an already scheduled timer.
-
timerWillRun
protected void timerWillRun(TimerFunction timer)
Introspection callback invoked before atimer
is executed.
-
runTimer
protected void runTimer(TimerFunction timer, Runnable runnable)
Invokestimer.runTimer()
, or arranges for the asynchronous execution of the providedrunnable
, which will itself invoketimer.runTimer()
.
-
timerDidRun
protected void timerDidRun(TimerFunction timer)
Introspection callback invoked after atimer
executes nominally.
-
timerDidFail
protected void timerDidFail(TimerFunction timer, Throwable error)
Introspection callback invoked after atimer
execution fails by throwing anerror
.
-
nanoTime
protected long nanoTime()
Returns the current time, in nanoseconds, with arbitrary origin. Used by the clock thread to determine the current time. Defaults toSystem.nanoTime()
. Can be overridden to substitute an alternative time source.
-
sleep
protected void sleep(long millis) throws InterruptedException
Parks the current thread for the specified number ofmillis
. Used by the clock thread to wait for the next clock tick. Defaults toThread.sleep(long)
. Can be overridden to substitute an alternative wait mechanism.- Throws:
InterruptedException
-
-