Class TimerWheels

  • All Implemented Interfaces:
    TimerStructure

    public class TimerWheels
    extends Object
    implements TimerStructure
    Hierarchical timer wheel implementation. The implementation is flexible and can work with variable delta time per time slot and variable slots per wheel level.

    This implementation is rather simple and has opportunities to improve performance.

    Author:
    Jens Wilke
    • Constructor Detail

      • TimerWheels

        public TimerWheels​(long startTime,
                           long delta,
                           int slots)
    • Method Detail

      • schedule

        public long schedule​(TimerTask task,
                             long time)
        Description copied from interface: TimerStructure
        Insert task. Scheduling might be not possible if tasks for the requested time have already be run.
        Specified by:
        schedule in interface TimerStructure
        Returns:
        0, if time is already in the past. Or a positive value indicating the execution time of the timer slot
      • removeNextToRun

        public TimerTask removeNextToRun​(long time)
        Description copied from interface: TimerStructure
        Return a task that is supposed to execute at the given time or earlier. This also moves the clock hand of the timer structure.

        It may rarely happen that a subsequent method call has an earlier time, in case the operating system schedule delays a thread until the next scheduler event happens.

        Specified by:
        removeNextToRun in interface TimerStructure
        Returns:
        a task or null, if no more tasks are scheduled for the given time
      • nextRun

        public long nextRun()
        Description copied from interface: TimerStructure
        Time of next run, or MAX_VALUE if no more tasks are scheduled
        Specified by:
        nextRun in interface TimerStructure