Class Clock

java.lang.Object
swim.concurrent.Clock
All Implemented Interfaces:
Schedule
Direct Known Subclasses:
StageClock

public class Clock extends Object implements Schedule
Hashed wheel timer Schedule.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final int
    Default number of ticks per clock revolution, used by the no-arg Clock() constructor.
    static final int
    Default number of milliseconds between clock ticks, used by the no-arg Clock() constructor.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Constructs a new Clock with a timer resolution of TICK_MILLIS milliseconds, and a clock period of TICK_COUNT ticks per revolution.
    Clock(int tickMillis, int tickCount)
    Constructs a new Clock with a timer resolution of tickMillis milliseconds, and a clock period of tickCount ticks per revolution.
    Clock(ClockDef clockDef)
    Constructs a new Clock with the timer resolution and clock period specified by the given clockDef.
  • Method Summary

    Modifier and Type
    Method
    Description
    protected void
    Lifecycle callback invoked if the timer thread throws a fatal error.
    protected void
    Lifecycle callback invoked after the clock thread starts.
    protected void
    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
    Returns the current time, in nanoseconds, with arbitrary origin.
    protected void
    runTimer(TimerFunction timer, Runnable runnable)
    Invokes timer.runTimer(), or arranges for the asynchronous execution of the provided runnable, which will itself invoke timer.runTimer().
    setTimer(long millis, TimerFunction timer)
    Schedules timer to execute after millis milliseconds have elapsed.
    protected void
    sleep(long millis)
    Parks the current thread for the specified number of millis.
    final void
    Ensures that this Clock is up and running, starting up the clock thread if it has not yet been started.
    final void
    Ensures that this Clock has been permanently stopped, shutting down the clock thread, if it's currently running.
    final long
    Returns the tick sequence number of the lowest clock tick that has yet to finish executing.
    Returns an unscheduled TimerRef bound to timer, which can later be used to schedule timer.
    protected void
    Introspection callback invoked after a timer has been explicitly cancelled; not invoked when a timer is implicitly cancelled, such as when rescheduling an already scheduled timer.
    protected void
    Introspection callback invoked after a timer execution fails by throwing an error.
    protected void
    Introspection callback invoked after a timer executes nominally.
    protected void
    Introspection callback invoked before a timer is executed.
    protected void
    timerWillSchedule(TimerFunction timer, long millis)
    Introspection callback invoked before a timer is scheduled for execution with a delay of millis milliseconds.
    protected void
    Lifecycle callback invoked before the clock thread starts.
    protected void
    Lifecycle callback invoked before the clock thread stops.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • TICK_MILLIS

      public static final int TICK_MILLIS
      Default number of milliseconds between clock ticks, used by the no-arg Clock() constructor. Defaults to the value of the swim.clock.tick.millis system property, if defined; otherwise defaults to 100 milliseconds.
    • TICK_COUNT

      public static final int TICK_COUNT
      Default number of ticks per clock revolution, used by the no-arg Clock() constructor. Defaults to the value of the swim.clock.tick.count system property, if defined; otherwise defaults to 512 clock ticks per revolution.
  • Constructor Details

    • Clock

      public Clock(int tickMillis, int tickCount)
      Constructs a new Clock with a timer resolution of tickMillis milliseconds, and a clock period of tickCount ticks per revolution.
    • Clock

      public Clock(ClockDef clockDef)
      Constructs a new Clock with the timer resolution and clock period specified by the given clockDef.
    • Clock

      public Clock()
      Constructs a new Clock with a timer resolution of TICK_MILLIS milliseconds, and a clock period of TICK_COUNT ticks 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 this Clock is up and running, starting up the clock thread if it has not yet been started.
      Throws:
      ScheduleException - if this Clock has been stopped.
    • stop

      public final void stop()
      Ensures that this Clock has been permanently stopped, shutting down the clock thread, if it's currently running. Upon return, this Clock is guaranteed to be in the stopped state.
    • timer

      public TimerRef timer(TimerFunction timer)
      Description copied from interface: Schedule
      Returns an unscheduled TimerRef bound to timer, which can later be used to schedule timer.
      Specified by:
      timer in interface Schedule
    • setTimer

      public TimerRef setTimer(long millis, TimerFunction timer)
      Description copied from interface: Schedule
      Schedules timer to execute after millis milliseconds have elapsed. Returns a TimerRef that can be used to check the status of, reschedule, and cancel timer.
      Specified by:
      setTimer in interface Schedule
    • 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. If waitedMillis 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 fatal error. The clock thread will stop after invoking didFail.
    • timerWillSchedule

      protected void timerWillSchedule(TimerFunction timer, long millis)
      Introspection callback invoked before a timer is scheduled for execution with a delay of millis milliseconds.
    • timerDidCancel

      protected void timerDidCancel(TimerFunction timer)
      Introspection callback invoked after a timer 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 a timer is executed.
    • runTimer

      protected void runTimer(TimerFunction timer, Runnable runnable)
      Invokes timer.runTimer(), or arranges for the asynchronous execution of the provided runnable, which will itself invoke timer.runTimer().
    • timerDidRun

      protected void timerDidRun(TimerFunction timer)
      Introspection callback invoked after a timer executes nominally.
    • timerDidFail

      protected void timerDidFail(TimerFunction timer, Throwable error)
      Introspection callback invoked after a timer execution fails by throwing an error.
    • nanoTime

      protected long nanoTime()
      Returns the current time, in nanoseconds, with arbitrary origin. Used by the clock thread to determine the current time. Defaults to System.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 of millis. Used by the clock thread to wait for the next clock tick. Defaults to Thread.sleep(long). Can be overridden to substitute an alternative wait mechanism.
      Throws:
      InterruptedException