java.lang.Object
swim.concurrent.Clock
- All Implemented Interfaces:
Schedule
- Direct Known Subclasses:
StageClock
Hashed wheel timer
Schedule.-
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionClock()Constructs a newClockwith a timer resolution ofTICK_MILLISmilliseconds, and a clock period ofTICK_COUNTticks per revolution.Clock(int tickMillis, int tickCount) Constructs a newClockwith a timer resolution oftickMillismilliseconds, and a clock period oftickCountticks per revolution.Constructs a newClockwith the timer resolution and clock period specified by the givenclockDef. -
Method Summary
Modifier and TypeMethodDescriptionprotected voidLifecycle callback invoked if the timer thread throws a fatalerror.protected voiddidStart()Lifecycle callback invoked after the clock thread starts.protected voiddidStop()Lifecycle callback invoked after the clock thread stops.protected voiddidTick(long tick, long waitedMillis) Introspection callback invoked after each tick of the clock.protected longnanoTime()Returns the current time, in nanoseconds, with arbitrary origin.protected voidrunTimer(TimerFunction timer, Runnable runnable) Invokestimer.runTimer(), or arranges for the asynchronous execution of the providedrunnable, which will itself invoketimer.runTimer().setTimer(long millis, TimerFunction timer) Schedulestimerto execute aftermillismilliseconds have elapsed.protected voidsleep(long millis) Parks the current thread for the specified number ofmillis.final voidstart()Ensures that thisClockis up and running, starting up the clock thread if it has not yet been started.final voidstop()Ensures that thisClockhas been permanently stopped, shutting down the clock thread, if it's currently running.final longtick()Returns the tick sequence number of the lowest clock tick that has yet to finish executing.timer(TimerFunction timer) Returns an unscheduledTimerRefbound totimer, which can later be used to scheduletimer.protected voidtimerDidCancel(TimerFunction timer) Introspection callback invoked after atimerhas been explicitly cancelled; not invoked when a timer is implicitly cancelled, such as when rescheduling an already scheduled timer.protected voidtimerDidFail(TimerFunction timer, Throwable error) Introspection callback invoked after atimerexecution fails by throwing anerror.protected voidtimerDidRun(TimerFunction timer) Introspection callback invoked after atimerexecutes nominally.protected voidtimerWillRun(TimerFunction timer) Introspection callback invoked before atimeris executed.protected voidtimerWillSchedule(TimerFunction timer, long millis) Introspection callback invoked before atimeris scheduled for execution with a delay ofmillismilliseconds.protected voidLifecycle callback invoked before the clock thread starts.protected voidwillStop()Lifecycle callback invoked before the clock thread stops.
-
Field Details
-
TICK_MILLIS
public static final int TICK_MILLISDefault number of milliseconds between clock ticks, used by the no-argClock()constructor. Defaults to the value of theswim.clock.tick.millissystem property, if defined; otherwise defaults to100milliseconds. -
TICK_COUNT
public static final int TICK_COUNTDefault number of ticks per clock revolution, used by the no-argClock()constructor. Defaults to the value of theswim.clock.tick.countsystem property, if defined; otherwise defaults to512clock ticks per revolution.
-
-
Constructor Details
-
Clock
public Clock(int tickMillis, int tickCount) Constructs a newClockwith a timer resolution oftickMillismilliseconds, and a clock period oftickCountticks per revolution. -
Clock
Constructs a newClockwith the timer resolution and clock period specified by the givenclockDef. -
Clock
public Clock()Constructs a newClockwith a timer resolution ofTICK_MILLISmilliseconds, and a clock period ofTICK_COUNTticks per revolution.
-
-
Method Details
-
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 thisClockis up and running, starting up the clock thread if it has not yet been started.- Throws:
ScheduleException- if thisClockhas been stopped.
-
stop
public final void stop()Ensures that thisClockhas been permanently stopped, shutting down the clock thread, if it's currently running. Upon return, thisClockis guaranteed to be in the stopped state. -
timer
Description copied from interface:ScheduleReturns an unscheduledTimerRefbound totimer, which can later be used to scheduletimer. -
setTimer
Description copied from interface:ScheduleSchedulestimerto execute aftermillismilliseconds have elapsed. Returns aTimerRefthat 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.tickis the sequence number of the tick that was executed;waitedMillisis the number of milliseconds the clock thread slept before executing the tick. IfwaitedMillisis negative, then the clock thread didn't start executing the tick until-waitedMillismilliseconds 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
Lifecycle callback invoked if the timer thread throws a fatalerror. The clock thread will stop after invokingdidFail. -
timerWillSchedule
Introspection callback invoked before atimeris scheduled for execution with a delay ofmillismilliseconds. -
timerDidCancel
Introspection callback invoked after atimerhas been explicitly cancelled; not invoked when a timer is implicitly cancelled, such as when rescheduling an already scheduled timer. -
timerWillRun
Introspection callback invoked before atimeris executed. -
runTimer
Invokestimer.runTimer(), or arranges for the asynchronous execution of the providedrunnable, which will itself invoketimer.runTimer(). -
timerDidRun
Introspection callback invoked after atimerexecutes nominally. -
timerDidFail
Introspection callback invoked after atimerexecution 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
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
-