Class Clock

  • All Implemented Interfaces:
    java.lang.Runnable

    public class Clock
    extends java.lang.Thread
    System clock ticking every second by default.
    • Nested Class Summary

      • Nested classes/interfaces inherited from class java.lang.Thread

        java.lang.Thread.State, java.lang.Thread.UncaughtExceptionHandler
    • Field Summary

      • Fields inherited from class java.lang.Thread

        MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void addListener​(ClockListener listener)
      Adds new clock listener.
      static Clock getDefault()
      Returns default instance of Clock object.
      boolean isTicking()
      Returns whether clock is ticking or not.
      void removeListener​(ClockListener listener)
      Removes already registered clock listener.
      void resumeClock()
      Starts clock again.
      void run()
      Main clock loop.
      void setTick​(long tick)
      Sets period of one clock tick.
      void suspendClock()
      Temporarily stops clock.
      • Methods inherited from class java.lang.Thread

        activeCount, checkAccess, clone, countStackFrames, currentThread, dumpStack, enumerate, getAllStackTraces, getContextClassLoader, getDefaultUncaughtExceptionHandler, getId, getName, getPriority, getStackTrace, getState, getThreadGroup, getUncaughtExceptionHandler, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, onSpinWait, resume, setContextClassLoader, setDaemon, setDefaultUncaughtExceptionHandler, setName, setPriority, setUncaughtExceptionHandler, sleep, sleep, start, stop, suspend, toString, yield
      • Methods inherited from class java.lang.Object

        equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Method Detail

      • getDefault

        public static Clock getDefault()
        Returns default instance of Clock object. Tick period is initially set to 1000 ms.
        Returns:
        The only instance of clock in the system.
      • setTick

        public void setTick​(long tick)
        Sets period of one clock tick.
        Parameters:
        tick - Clock period i.e. time between two notifications to listeners.
      • addListener

        public void addListener​(ClockListener listener)
        Adds new clock listener.
        Parameters:
        listener - Object who whats to be notified after each tick.
      • removeListener

        public void removeListener​(ClockListener listener)
        Removes already registered clock listener.
        Parameters:
        listener - Object who no longer whats to be notified after each tick.
      • run

        public void run()
        Main clock loop. Nothing happens for given tick period and then all listeners are notified about that fact.
        Specified by:
        run in interface java.lang.Runnable
        Overrides:
        run in class java.lang.Thread
      • suspendClock

        public void suspendClock()
        Temporarily stops clock.
      • resumeClock

        public void resumeClock()
        Starts clock again.
      • isTicking

        public boolean isTicking()
        Returns whether clock is ticking or not.
        Returns:
        True if clock is ticking or false otherwise.