- 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 intTICK_COUNTDefault number of ticks per clock revolution, used by the no-argClock()constructor.static intTICK_MILLISDefault number of milliseconds between clock ticks, used by the no-argClock()constructor.
-
Constructor Summary
Constructors Constructor Description Clock()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.Clock(ClockDef clockDef)Constructs a newClockwith the timer resolution and clock period specified by the givenclockDef.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected voiddidFail(Throwable error)Lifecycle 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().TimerRefsetTimer(long millis, TimerFunction timer)Schedulestimerto execute aftermillismilliseconds have elapsed.protected voidsleep(long millis)Parks the current thread for the specified number ofmillis.voidstart()Ensures that thisClockis up and running, starting up the clock thread if it has not yet been started.voidstop()Ensures that thisClockhas been permanently stopped, shutting down the clock thread, if it's currently running.longtick()Returns the tick sequence number of the lowest clock tick that has yet to finish executing.TimerReftimer(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 voidwillStart()Lifecycle callback invoked before the clock thread starts.protected voidwillStop()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.millissystem property, if defined; otherwise defaults to100milliseconds.
-
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.countsystem property, if defined; otherwise defaults to512clock ticks per revolution.
-
-
Constructor Detail
-
Clock
public Clock(int tickMillis, int tickCount)Constructs a newClockwith a timer resolution oftickMillismilliseconds, and a clock period oftickCountticks per revolution.
-
Clock
public Clock(ClockDef clockDef)
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 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 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
public TimerRef timer(TimerFunction timer)
Description copied from interface:ScheduleReturns an unscheduledTimerRefbound totimer, which can later be used to scheduletimer.
-
setTimer
public TimerRef setTimer(long millis, TimerFunction timer)
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
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 atimeris scheduled for execution with a delay ofmillismilliseconds.
-
timerDidCancel
protected void timerDidCancel(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.
-
timerWillRun
protected void timerWillRun(TimerFunction timer)
Introspection callback invoked before atimeris 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 atimerexecutes nominally.
-
timerDidFail
protected void timerDidFail(TimerFunction timer, Throwable error)
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
protected void sleep(long millis) throws InterruptedExceptionParks 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
-
-