public interface TimerWheel
Represents a Timer Wheel data structure designed for use in games with a consistent tick duration.
The wheel holds various
There are 2 provided implementations,
The wheel holds various
Tasks that are scheduled to execute at
a specific point in the future, in sync with the game's main loop.
Internally the wheel utilizes one or more circular arrays that rotate every time advance()
is called.
There are 2 provided implementations,
Simple and
Hierarchical.-
Method Summary
Modifier and TypeMethodDescriptionvoidadvance()Advance this wheel by a single tick, performing all necessary bookkeeping and removing or rescheduling any expired tasks.intGet the current tick tracked by the wheel.static TimerWheelCreate a hierarchical timer wheel suited for variable expiration.default TaskCreate and schedule a task on this timer wheel.<T extends Expiring>
Tschedule(T task, int delay) Schedule a task on this timer wheel.voidshutdown(boolean run) Shutdown this timer wheel by clearing all scheduled tasks.static TimerWheelsimple(int capacity) Create a simple timer wheel for tasks that expire shortly.
-
Method Details
-
currentTick
int currentTick()Get the current tick tracked by the wheel.- Returns:
- the current tick
-
advance
void advance()Advance this wheel by a single tick, performing all necessary bookkeeping and removing or rescheduling any expired tasks. -
schedule
Create and schedule a task on this timer wheel.- Parameters:
consumer- the task to scheduledelay- the delay in game ticksperiod- the repeat period in game ticks- Returns:
- the scheduled task
- See Also:
-
schedule
Schedule a task on this timer wheel.- Type Parameters:
T- the type of task- Parameters:
task- the task to scheduledelay- the delay in game ticks- Returns:
- the scheduled task
- See Also:
-
shutdown
void shutdown(boolean run) Shutdown this timer wheel by clearing all scheduled tasks.- Parameters:
run- whether to run each expiring task when cleared
-
hierarchical
Create a hierarchical timer wheel suited for variable expiration.- Returns:
- the timer wheel instance
-
simple
Create a simple timer wheel for tasks that expire shortly. This offers better performance than aHierarchicalwheel by eliminating bookkeeping and wheel cascading at the cost of memory. For that reason, the wheel's capacity will be limited to the given size.- Parameters:
capacity- the wheel's max capacity in ticks- Returns:
- the timer wheel instance
-