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 boolean 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:
        true if scheduled successfully, false if scheduling was not possible because the target time slot would be in the past
      • 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 -1 if no more tasks are scheduled
        Specified by:
        nextRun in interface TimerStructure