Interface TimerWheel


public interface TimerWheel
Represents a Timer Wheel data structure designed for use in games with a consistent tick duration.
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.
See Also:
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Advance this wheel by a single tick, performing all necessary bookkeeping and removing or rescheduling any expired tasks.
    int
    Get the current tick tracked by the wheel.
    static TimerWheel
    Create a hierarchical timer wheel suited for variable expiration.
    default Task
    schedule(Consumer<? super Task> consumer, int delay, int period)
    Create and schedule a task on this timer wheel.
    <T extends Expiring>
    T
    schedule(T task, int delay)
    Schedule a task on this timer wheel.
    void
    shutdown(boolean run)
    Shutdown this timer wheel by clearing all scheduled tasks.
    static TimerWheel
    simple(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

      default Task schedule(Consumer<? super Task> consumer, int delay, int period)
      Create and schedule a task on this timer wheel.
      Parameters:
      consumer - the task to schedule
      delay - the delay in game ticks
      period - the repeat period in game ticks
      Returns:
      the scheduled task
      See Also:
    • schedule

      <T extends Expiring> T schedule(T task, int delay)
      Schedule a task on this timer wheel.
      Type Parameters:
      T - the type of task
      Parameters:
      task - the task to schedule
      delay - 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

      static TimerWheel hierarchical()
      Create a hierarchical timer wheel suited for variable expiration.
      Returns:
      the timer wheel instance
    • simple

      static TimerWheel simple(int capacity)
      Create a simple timer wheel for tasks that expire shortly. This offers better performance than a Hierarchical wheel 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