- java.lang.Object
-
- io.netty5.util.concurrent.AbstractEventExecutor
-
- io.netty5.util.concurrent.AbstractScheduledEventExecutor
-
- All Implemented Interfaces:
EventExecutor,EventExecutorGroup,FuturePromiseFactory,Iterable<EventExecutor>,Executor
- Direct Known Subclasses:
GlobalEventExecutor,SingleThreadEventExecutor
public abstract class AbstractScheduledEventExecutor extends AbstractEventExecutor
Abstract base class forEventExecutors that want to support scheduling.
-
-
Constructor Summary
Constructors Modifier Constructor Description protectedAbstractScheduledEventExecutor()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description protected voidcancelScheduledTasks()Cancel all scheduled tasks.protected longgetCurrentTimeNanos()Get the current time in nanoseconds by this executor's clock.protected booleanhasScheduledTasks()Returnstrueif a scheduled task is ready for processing.protected static longinitialNanoTime()The initial value used for delay and computations based upon a monatomic time source.static longnanoTime()The time elapsed since initialization of this class in nanoseconds.protected static <V> RunnableScheduledFuture<V>newRunnableScheduledFuture(AbstractScheduledEventExecutor executor, Promise<V> promise, Callable<V> task, long deadlineNanos, long periodNanos)protected <V> RunnableScheduledFuture<V>newScheduledTaskFor(Callable<V> callable, long deadlineNanos, long period)Returns aRunnableScheduledFuturefor the given values.protected longnextScheduledTaskNano()Return the nanoseconds when the next scheduled task is ready to be run or-1if no task is scheduled.protected RunnableScheduledFuture<?>pollScheduledTask()protected RunnableScheduledFuture<?>pollScheduledTask(long nanoTime)Return theRunnablewhich is ready to be executed with the givennanoTime.protected <V> Future<V>schedule(RunnableScheduledFuture<V> task)Add theRunnableScheduledFuturefor execution.Future<Void>schedule(Runnable command, long delay, TimeUnit unit)Schedule the given task for execution after the given delay, in the next availableEventExecutorin this group, and return a future that produces anullresult when the task completes.<V> Future<V>schedule(Callable<V> callable, long delay, TimeUnit unit)Schedule the given task for execution after the given delay, in the next availableEventExecutorin this group, and return a future that will return the result of the callable when the task completes.Future<Void>scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit)Schedule the given task for periodic execution in the next availableEventExecutor.Future<Void>scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit)Schedule the given task for periodic execution in the next availableEventExecutor.-
Methods inherited from class io.netty5.util.concurrent.AbstractEventExecutor
newSucceededFuture, newTaskFor, newTaskFor, submit, submit, submit
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface io.netty5.util.concurrent.EventExecutor
execute, inEventLoop, inEventLoop, iterator, newFailedFuture, newPromise, next
-
Methods inherited from interface io.netty5.util.concurrent.EventExecutorGroup
awaitTermination, isShutdown, isShuttingDown, isTerminated, shutdownGracefully, shutdownGracefully, terminationFuture
-
Methods inherited from interface io.netty5.util.concurrent.FuturePromiseFactory
newSucceededFuture
-
Methods inherited from interface java.lang.Iterable
forEach, spliterator
-
-
-
-
Method Detail
-
nanoTime
public static long nanoTime()
The time elapsed since initialization of this class in nanoseconds. This may return a negative number just likeSystem.nanoTime().
-
initialNanoTime
protected static long initialNanoTime()
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.
-
getCurrentTimeNanos
protected long getCurrentTimeNanos()
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
-
cancelScheduledTasks
protected final void cancelScheduledTasks()
Cancel all scheduled tasks.This method MUST be called only when
EventExecutor.inEventLoop()istrue.
-
pollScheduledTask
protected final RunnableScheduledFuture<?> pollScheduledTask()
- See Also:
pollScheduledTask(long)
-
pollScheduledTask
protected final RunnableScheduledFuture<?> pollScheduledTask(long nanoTime)
Return theRunnablewhich is ready to be executed with the givennanoTime. You should usegetCurrentTimeNanos()to retrieve the correctnanoTime.This method MUST be called only when
EventExecutor.inEventLoop()istrue.
-
nextScheduledTaskNano
protected final long nextScheduledTaskNano()
Return the nanoseconds when the next scheduled task is ready to be run or-1if no task is scheduled.This method MUST be called only when
EventExecutor.inEventLoop()istrue.
-
hasScheduledTasks
protected final boolean hasScheduledTasks()
Returnstrueif a scheduled task is ready for processing.This method MUST be called only when
EventExecutor.inEventLoop()istrue.
-
schedule
public Future<Void> schedule(Runnable command, long delay, TimeUnit unit)
Description copied from interface:EventExecutorGroupSchedule the given task for execution after the given delay, in the next availableEventExecutorin this group, and return a future that produces anullresult when the task completes.- Parameters:
command- The task that should be executed in thisEventExecutorGroupafter the given delay.delay- A positive time delay, in the given time unit.unit- The non-null time unit for the delay.- Returns:
- A future that represents the completion of the scheduled task.
-
schedule
public <V> Future<V> schedule(Callable<V> callable, long delay, TimeUnit unit)
Description copied from interface:EventExecutorGroupSchedule the given task for execution after the given delay, in the next availableEventExecutorin this group, and return a future that will return the result of the callable when the task completes.- Type Parameters:
V- The type of the future result.- Parameters:
callable- The task that should be executed in thisEventExecutorGroupafter the given delay.delay- A positive time delay, in the given time unit.unit- The non-null time unit for the delay.- Returns:
- A future that represents the completion of the scheduled task.
-
scheduleAtFixedRate
public Future<Void> scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit)
Description copied from interface:EventExecutorGroupSchedule the given task for periodic execution in the next availableEventExecutor. The first execution will occur after the given initial delay, and the following repeated executions will occur with the given period of time between each execution is started. If the task takes longer to complete than the requested period, then the following executions will be delayed, rather than allowing multiple instances of the task to run concurrently.The task will be executed repeatedly until it either fails with an exception, or its future is cancelled. The future thus will never complete successfully.
- Parameters:
command- The task that should be scheduled to execute at a fixed rate in thisEventExecutorGroup.initialDelay- The positive initial delay for the first task execution, in terms of the given time unit.period- The positive period for the execution frequency to use after the first execution has started, in terms of the given time unit.unit- The non-null time unit for the delay and period.- Returns:
- A future that represents the recurring task, and which can be cancelled to stop future executions.
-
scheduleWithFixedDelay
public Future<Void> scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit)
Description copied from interface:EventExecutorGroupSchedule the given task for periodic execution in the next availableEventExecutor. The first execution will occur after the given initial delay, and the following repeated executions will occur with the given subsequent delay between one task completing and the next task starting. The delay from the completion of one task, to the start of the next, stays unchanged regardless of how long a task takes to complete.This is in contrast to
EventExecutorGroup.scheduleAtFixedRate(Runnable, long, long, TimeUnit)which varies the delays between the tasks in order to hit a given frequency.The task will be executed repeatedly until it either fails with an exception, or its future is cancelled. The future thus will never complete successfully.
- Parameters:
command- The task that should be scheduled to execute with fixed delays in thisEventExecutorGroup.initialDelay- The positive initial delay for the first task execution, in terms of the given time unit.delay- The positive subsequent delay between task, to use after the first execution has completed, in terms of the given time unit.unit- The non-null time unit for the delays.- Returns:
- A future that represents the recurring task, and which can be cancelled to stop future executions.
-
schedule
protected final <V> Future<V> schedule(RunnableScheduledFuture<V> task)
Add theRunnableScheduledFuturefor execution.
-
newRunnableScheduledFuture
protected static <V> RunnableScheduledFuture<V> newRunnableScheduledFuture(AbstractScheduledEventExecutor executor, Promise<V> promise, Callable<V> task, long deadlineNanos, long periodNanos)
Returns a newRunnableFuturebuild on top of the givenPromiseandCallable.This can be used if you want to override
newScheduledTaskFor(Callable, long, long)and return a differentRunnableFuture.
-
newScheduledTaskFor
protected <V> RunnableScheduledFuture<V> newScheduledTaskFor(Callable<V> callable, long deadlineNanos, long period)
Returns aRunnableScheduledFuturefor the given values.
-
-