Package org.cesilko.rachota.core
Class Clock
java.lang.Object
java.lang.Thread
org.cesilko.rachota.core.Clock
- All Implemented Interfaces:
Runnable
System clock ticking every second by default.
-
Nested Class Summary
Nested classes/interfaces inherited from class java.lang.Thread
Thread.Builder, Thread.State, Thread.UncaughtExceptionHandler
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static Clock
The only instance of clock object in the system.private Vector
Set of clock listeners that want to be notified at each tick.private long
Clock period i.e.private boolean
True if clock is ticking.Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
addListener
(ClockListener listener) Adds new clock listener.static Clock
Returns default instance of Clock object.boolean
Returns whether clock is ticking or not.void
removeListener
(ClockListener listener) Removes already registered clock listener.void
Starts clock again.void
run()
Main clock loop.void
setTick
(long tick) Sets period of one clock tick.void
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, isVirtual, join, join, join, join, ofPlatform, ofVirtual, onSpinWait, resume, setContextClassLoader, setDaemon, setDefaultUncaughtExceptionHandler, setName, setPriority, setUncaughtExceptionHandler, sleep, sleep, sleep, start, startVirtualThread, stop, suspend, threadId, toString, yield
-
Field Details
-
tick
private long tickClock period i.e. time gap between two ticks. -
listeners
Set of clock listeners that want to be notified at each tick. -
ticking
private boolean tickingTrue if clock is ticking. -
clock
The only instance of clock object in the system.
-
-
Constructor Details
-
Clock
private Clock(long tick) Clock constructor.- Parameters:
tick
- Required clock tick in milliseconds.
-
-
Method Details
-
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
Adds new clock listener.- Parameters:
listener
- Object who whats to be notified after each tick.
-
removeListener
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. -
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.
-