Class AbstractScheduledEventExecutor
java.lang.Object
java.util.concurrent.AbstractExecutorService
io.netty.util.concurrent.AbstractEventExecutor
io.netty.util.concurrent.AbstractScheduledEventExecutor
- All Implemented Interfaces:
EventExecutor, EventExecutorGroup, ThreadAwareExecutor, java.lang.Iterable<EventExecutor>, java.util.concurrent.Executor, java.util.concurrent.ExecutorService, java.util.concurrent.ScheduledExecutorService
- Direct Known Subclasses:
EmbeddedEventLoop, GlobalEventExecutor, ManualIoEventLoop, SingleThreadEventExecutor
Abstract base class for
EventExecutors that want to support scheduling.-
Nested Class Summary
Nested classes/interfaces inherited from class AbstractEventExecutor
AbstractEventExecutor.LazyRunnable -
Field Summary
FieldsModifier and TypeFieldDescription(package private) longprivate static final java.util.Comparator<ScheduledFutureTask<?>> (package private) PriorityQueue<ScheduledFutureTask<?>> (package private) static final java.lang.RunnableFields inherited from class AbstractEventExecutor
DEFAULT_SHUTDOWN_QUIET_PERIOD, DEFAULT_SHUTDOWN_TIMEOUT -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedprotected -
Method Summary
Modifier and TypeMethodDescriptionprotected booleanafterScheduledTaskSubmitted(long deadlineNanos) protected booleanbeforeScheduledTaskSubmitted(long deadlineNanos) Called from arbitrary non-EventExecutorthreads prior to scheduled task submission.protected voidCancel all scheduled tasks.(package private) static longdeadlineNanos(long nanoTime, long delay) protected static longdeadlineToDelayNanos(long deadlineNanos) Deprecated.(package private) static longDeprecated.Use the non-staticticker()instead.protected longdelayNanos(long currentTimeNanos, long scheduledPurgeInterval) Returns the amount of time left until the scheduled task with the closest dead line is executed.protected booleanfetchFromScheduledTaskQueue(java.util.Queue<java.lang.Runnable> taskQueue) Fetch scheduled tasks from the internal queue and add these to the givenQueue.protected longDeprecated.Please use (or override)ticker()instead.protected final booleanReturnstrueif a scheduled task is ready for processing.protected static longDeprecated.Useticker()insteadprivate static booleanisNullOrEmpty(java.util.Queue<ScheduledFutureTask<?>> queue) protected static longnanoTime()Deprecated.Use the non-staticticker()instead.protected final longReturn the deadline (in nanoseconds) when the next scheduled task is ready to be run or-1if no task is scheduled.protected final longReturn the nanoseconds until the next scheduled task is ready to be run or-1if no task is scheduled.(package private) final ScheduledFutureTask<?> protected final java.lang.Runnableprotected final java.lang.RunnablepollScheduledTask(long nanoTime) Return theRunnablewhich is ready to be executed with the givennanoTime.(package private) final voidremoveScheduled(ScheduledFutureTask<?> task) private <V> ScheduledFuture<V> schedule(ScheduledFutureTask<V> task) schedule(java.lang.Runnable command, long delay, java.util.concurrent.TimeUnit unit) <V> ScheduledFuture<V> schedule(java.util.concurrent.Callable<V> callable, long delay, java.util.concurrent.TimeUnit unit) scheduleAtFixedRate(java.lang.Runnable command, long initialDelay, long period, java.util.concurrent.TimeUnit unit) (package private) PriorityQueue<ScheduledFutureTask<?>> (package private) final voidscheduleFromEventLoop(ScheduledFutureTask<?> task) (package private) voidscheduleWithFixedDelay(java.lang.Runnable command, long initialDelay, long delay, java.util.concurrent.TimeUnit unit) ticker()The ticker for this executor.protected voidvalidateScheduled(long amount, java.util.concurrent.TimeUnit unit) Deprecated.will be removed in the future.private voidvalidateScheduled0(long amount, java.util.concurrent.TimeUnit unit) Methods inherited from class AbstractEventExecutor
iterator, lazyExecute, newTaskFor, newTaskFor, next, parent, runTask, safeExecute, shutdown, shutdownGracefully, shutdownNow, submit, submit, submitMethods inherited from class java.util.concurrent.AbstractExecutorService
invokeAll, invokeAll, invokeAny, invokeAnyMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface EventExecutor
inEventLoop, inEventLoop, isExecutorThread, isSuspended, newFailedFuture, newProgressivePromise, newPromise, newSucceededFuture, trySuspendMethods inherited from interface EventExecutorGroup
isShuttingDown, shutdownGracefully, terminationFutureMethods inherited from interface java.util.concurrent.Executor
executeMethods inherited from interface java.util.concurrent.ExecutorService
awaitTermination, invokeAll, invokeAll, invokeAny, invokeAny, isShutdown, isTerminatedMethods inherited from interface java.lang.Iterable
forEach, spliterator
-
Field Details
-
SCHEDULED_FUTURE_TASK_COMPARATOR
-
WAKEUP_TASK
static final java.lang.Runnable WAKEUP_TASK -
scheduledTaskQueue
PriorityQueue<ScheduledFutureTask<?>> scheduledTaskQueue -
nextTaskId
long nextTaskId
-
-
Constructor Details
-
AbstractScheduledEventExecutor
protected AbstractScheduledEventExecutor() -
AbstractScheduledEventExecutor
-
-
Method Details
-
ticker
Description copied from interface:EventExecutorGroupThe ticker for this executor. Usually theEventExecutorGroup.schedule(Runnable, long, TimeUnit)methods will follow thesystem 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.Get the current time in nanoseconds by this executor's clock. This is not the same asSystem.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.
- We apply a fixed offset to the
-
nanoTime
@Deprecated protected static long nanoTime()Deprecated.Use the non-staticticker()instead. -
defaultCurrentTimeNanos
@Deprecated static long defaultCurrentTimeNanos()Deprecated.Use the non-staticticker()instead. -
deadlineNanos
static long deadlineNanos(long nanoTime, long delay) -
deadlineToDelayNanos
@Deprecated protected static long deadlineToDelayNanos(long deadlineNanos) Deprecated.Useticker()insteadGiven an arbitrary deadlinedeadlineNanos, calculate the number of nano seconds from nowdeadlineNanoswould expire.- Parameters:
deadlineNanos- An arbitrary deadline in nano seconds.- Returns:
- the number of nano seconds from now
deadlineNanoswould 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.Useticker()insteadThe 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.
-
scheduledTaskQueue
PriorityQueue<ScheduledFutureTask<?>> scheduledTaskQueue() -
isNullOrEmpty
-
cancelScheduledTasks
protected void cancelScheduledTasks()Cancel all scheduled tasks. This method MUST be called only whenEventExecutor.inEventLoop()istrue. -
pollScheduledTask
protected final java.lang.Runnable pollScheduledTask()- See Also:
-
fetchFromScheduledTaskQueue
protected boolean fetchFromScheduledTaskQueue(java.util.Queue<java.lang.Runnable> taskQueue) Fetch scheduled tasks from the internal queue and add these to the givenQueue.- Parameters:
taskQueue- the task queue into which the fetched scheduled tasks should be transferred.- Returns:
trueif we were able to transfer everything,falseif we need to call this method again as soon as there is space again intaskQueue.
-
pollScheduledTask
protected final java.lang.Runnable pollScheduledTask(long nanoTime) Return theRunnablewhich is ready to be executed with the givennanoTime. You should usegetCurrentTimeNanos()to retrieve the correctnanoTime. -
nextScheduledTaskNano
protected final long nextScheduledTaskNano()Return the nanoseconds until the next scheduled task is ready to be run or-1if 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-1if no task is scheduled. -
peekScheduledTask
-
hasScheduledTasks
protected final boolean hasScheduledTasks()Returnstrueif a scheduled task is ready for processing. -
schedule
public ScheduledFuture<?> schedule(java.lang.Runnable command, long delay, java.util.concurrent.TimeUnit unit) - Specified by:
schedulein interfaceEventExecutorGroup- Specified by:
schedulein interfacejava.util.concurrent.ScheduledExecutorService- Overrides:
schedulein classAbstractEventExecutor
-
schedule
public <V> ScheduledFuture<V> schedule(java.util.concurrent.Callable<V> callable, long delay, java.util.concurrent.TimeUnit unit) - Specified by:
schedulein interfaceEventExecutorGroup- Specified by:
schedulein interfacejava.util.concurrent.ScheduledExecutorService- Overrides:
schedulein classAbstractEventExecutor
-
scheduleAtFixedRate
public ScheduledFuture<?> scheduleAtFixedRate(java.lang.Runnable command, long initialDelay, long period, java.util.concurrent.TimeUnit unit) - Specified by:
scheduleAtFixedRatein interfaceEventExecutorGroup- Specified by:
scheduleAtFixedRatein interfacejava.util.concurrent.ScheduledExecutorService- Overrides:
scheduleAtFixedRatein classAbstractEventExecutor
-
scheduleWithFixedDelay
public ScheduledFuture<?> scheduleWithFixedDelay(java.lang.Runnable command, long initialDelay, long delay, java.util.concurrent.TimeUnit unit) - Specified by:
scheduleWithFixedDelayin interfaceEventExecutorGroup- Specified by:
scheduleWithFixedDelayin interfacejava.util.concurrent.ScheduledExecutorService- Overrides:
scheduleWithFixedDelayin classAbstractEventExecutor
-
validateScheduled0
private void validateScheduled0(long amount, java.util.concurrent.TimeUnit unit) -
validateScheduled
@Deprecated protected void validateScheduled(long amount, java.util.concurrent.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. -
scheduleFromEventLoop
-
schedule
-
removeScheduled
-
scheduleRemoveScheduled
-
beforeScheduledTaskSubmitted
protected boolean beforeScheduledTaskSubmitted(long deadlineNanos) Called from arbitrary non-EventExecutorthreads prior to scheduled task submission. Returnstrueif theEventExecutorthread should be woken immediately to process the scheduled task (if not already awake).If
falseis returned,afterScheduledTaskSubmitted(long)will be called with the same value after the scheduled task is enqueued, providing another opportunity to wake theEventExecutorthread if required.- Parameters:
deadlineNanos- deadline of the to-be-scheduled task relative togetCurrentTimeNanos()- Returns:
trueif theEventExecutorthread should be woken,falseotherwise
-
afterScheduledTaskSubmitted
protected boolean afterScheduledTaskSubmitted(long deadlineNanos) SeebeforeScheduledTaskSubmitted(long). Called only after that method returns false.- Parameters:
deadlineNanos- relative togetCurrentTimeNanos()- Returns:
trueif theEventExecutorthread should be woken,falseotherwise
-
ticker()instead