Class AbstractScheduledEventExecutor

All Implemented Interfaces:
EventExecutor, EventExecutorGroup, ThreadAwareExecutor, Iterable<EventExecutor>, Executor, ExecutorService, ScheduledExecutorService
Direct Known Subclasses:
GlobalEventExecutor, ManualIoEventLoop, SingleThreadEventExecutor

public abstract class AbstractScheduledEventExecutor extends AbstractEventExecutor
Abstract base class for EventExecutors that want to support scheduling.
  • Constructor Details

    • AbstractScheduledEventExecutor

      protected AbstractScheduledEventExecutor()
    • AbstractScheduledEventExecutor

      protected AbstractScheduledEventExecutor(EventExecutorGroup parent)
  • Method Details

    • ticker

      public Ticker ticker()
      Description copied from interface: EventExecutorGroup
      The ticker for this executor. Usually the EventExecutorGroup.schedule(Runnable, long, TimeUnit) methods will follow the system ticker (i.e. System.nanoTime()), but especially for testing it is sometimes useful to have more control over the ticker. In that case, this method will be overridden. Code that schedules tasks on this executor should use this ticker in order to stay consistent with the executor (e.g. not be surprised by scheduled tasks running "early").
      Returns:
      The ticker for this scheduler
    • getCurrentTimeNanos

      @Deprecated protected long getCurrentTimeNanos()
      Deprecated.
      Please use (or override) ticker() instead. This method delegates to ticker(). Old code may still call this method for compatibility.
      Get the current time in nanoseconds by this executor's clock. This is not the same as System.nanoTime() for two reasons:
      • We apply a fixed offset to the nanoTime
      • Implementations (in particular EmbeddedEventLoop) may use their own time source so they can control time for testing purposes.
    • nanoTime

      @Deprecated protected static long nanoTime()
      Deprecated.
      Use the non-static ticker() instead.
    • deadlineToDelayNanos

      @Deprecated protected static long deadlineToDelayNanos(long deadlineNanos)
      Deprecated.
      Use ticker() instead
      Given an arbitrary deadline deadlineNanos, calculate the number of nano seconds from now deadlineNanos would expire.
      Parameters:
      deadlineNanos - An arbitrary deadline in nano seconds.
      Returns:
      the number of nano seconds from now deadlineNanos would expire.
    • delayNanos

      protected long delayNanos(long currentTimeNanos, long scheduledPurgeInterval)
      Returns the amount of time left until the scheduled task with the closest dead line is executed.
    • initialNanoTime

      @Deprecated protected static long initialNanoTime()
      Deprecated.
      Use ticker() instead
      The initial value used for delay and computations based upon a monatomic time source.
      Returns:
      initial value used for delay and computations based upon a monatomic time source.
    • cancelScheduledTasks

      protected void cancelScheduledTasks()
      Cancel all scheduled tasks. This method MUST be called only when EventExecutor.inEventLoop() is true.
    • pollScheduledTask

      protected final Runnable pollScheduledTask()
      See Also:
    • fetchFromScheduledTaskQueue

      protected boolean fetchFromScheduledTaskQueue(Queue<Runnable> taskQueue)
      Fetch scheduled tasks from the internal queue and add these to the given Queue.
      Parameters:
      taskQueue - the task queue into which the fetched scheduled tasks should be transferred.
      Returns:
      true if we were able to transfer everything, false if we need to call this method again as soon as there is space again in taskQueue.
    • pollScheduledTask

      protected final Runnable pollScheduledTask(long nanoTime)
      Return the Runnable which is ready to be executed with the given nanoTime. You should use getCurrentTimeNanos() to retrieve the correct nanoTime.
    • nextScheduledTaskNano

      protected final long nextScheduledTaskNano()
      Return the nanoseconds until the next scheduled task is ready to be run or -1 if no task is scheduled.
    • nextScheduledTaskDeadlineNanos

      protected final long nextScheduledTaskDeadlineNanos()
      Return the deadline (in nanoseconds) when the next scheduled task is ready to be run or -1 if no task is scheduled.
    • hasScheduledTasks

      protected final boolean hasScheduledTasks()
      Returns true if a scheduled task is ready for processing.
    • schedule

      public ScheduledFuture<?> schedule(Runnable command, long delay, TimeUnit unit)
      Specified by:
      schedule in interface EventExecutorGroup
      Specified by:
      schedule in interface ScheduledExecutorService
      Overrides:
      schedule in class AbstractEventExecutor
    • schedule

      public <V> ScheduledFuture<V> schedule(Callable<V> callable, long delay, TimeUnit unit)
      Specified by:
      schedule in interface EventExecutorGroup
      Specified by:
      schedule in interface ScheduledExecutorService
      Overrides:
      schedule in class AbstractEventExecutor
    • scheduleAtFixedRate

      public ScheduledFuture<?> scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit)
      Specified by:
      scheduleAtFixedRate in interface EventExecutorGroup
      Specified by:
      scheduleAtFixedRate in interface ScheduledExecutorService
      Overrides:
      scheduleAtFixedRate in class AbstractEventExecutor
    • scheduleWithFixedDelay

      public ScheduledFuture<?> scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit)
      Specified by:
      scheduleWithFixedDelay in interface EventExecutorGroup
      Specified by:
      scheduleWithFixedDelay in interface ScheduledExecutorService
      Overrides:
      scheduleWithFixedDelay in class AbstractEventExecutor
    • validateScheduled

      @Deprecated protected void validateScheduled(long amount, TimeUnit unit)
      Deprecated.
      will be removed in the future.
      Sub-classes may override this to restrict the maximal amount of time someone can use to schedule a task.
    • beforeScheduledTaskSubmitted

      protected boolean beforeScheduledTaskSubmitted(long deadlineNanos)
      Called from arbitrary non-EventExecutor threads prior to scheduled task submission. Returns true if the EventExecutor thread should be woken immediately to process the scheduled task (if not already awake).

      If false is returned, afterScheduledTaskSubmitted(long) will be called with the same value after the scheduled task is enqueued, providing another opportunity to wake the EventExecutor thread if required.

      Parameters:
      deadlineNanos - deadline of the to-be-scheduled task relative to getCurrentTimeNanos()
      Returns:
      true if the EventExecutor thread should be woken, false otherwise
    • afterScheduledTaskSubmitted

      protected boolean afterScheduledTaskSubmitted(long deadlineNanos)
      See beforeScheduledTaskSubmitted(long). Called only after that method returns false.
      Parameters:
      deadlineNanos - relative to getCurrentTimeNanos()
      Returns:
      true if the EventExecutor thread should be woken, false otherwise