- java.lang.Object
-
- io.netty5.util.concurrent.AbstractEventExecutor
-
- io.netty5.util.concurrent.AbstractScheduledEventExecutor
-
- io.netty5.util.concurrent.SingleThreadEventExecutor
-
- All Implemented Interfaces:
EventExecutor,EventExecutorGroup,FuturePromiseFactory,OrderedEventExecutor,Iterable<EventExecutor>,Executor
- Direct Known Subclasses:
SingleThreadEventLoop
public class SingleThreadEventExecutor extends AbstractScheduledEventExecutor implements OrderedEventExecutor
OrderedEventExecutor's implementation that execute all its submitted tasks in a single thread.
-
-
Field Summary
Fields Modifier and Type Field Description protected static intDEFAULT_MAX_PENDING_EXECUTOR_TASKS
-
Constructor Summary
Constructors Constructor Description SingleThreadEventExecutor()Create a new instanceSingleThreadEventExecutor(Executor executor)Create a new instanceSingleThreadEventExecutor(Executor executor, int maxPendingTasks, RejectedExecutionHandler rejectedHandler)Create a new instanceSingleThreadEventExecutor(ThreadFactory threadFactory)Create a new instanceSingleThreadEventExecutor(ThreadFactory threadFactory, int maxPendingTasks, RejectedExecutionHandler rejectedHandler)Create a new instance
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddShutdownHook(Runnable task)Add aRunnablewhich will be executed on shutdown of this instancebooleanawaitTermination(long timeout, TimeUnit unit)Wait for thisEventExecutorGroupto terminate, up to the given timeout.protected voidcleanup()Do nothing, sub-classes may override.protected booleanconfirmShutdown()Confirm that the shutdown if the instance should be done now!protected longdeadlineNanos()Returns the absolute point in time (relative toAbstractScheduledEventExecutor.getCurrentTimeNanos()()}) at which the next closest scheduled task should run or-1if none is scheduled at the mment.protected longdelayNanos(long currentTimeNanos)Returns the amount of time left until the scheduled task with the closest dead line is executed.voidexecute(Runnable task)protected booleanhasTasks()booleaninEventLoop(Thread thread)protected voidinterruptThread()Interrupt the current runningThread.booleanisShutdown()Returnstrueif allEventExecutors managed by thisEventExecutorGrouphave been shut down gracefully and moved past the grace period so that they are no longer accepting any new tasks.booleanisShuttingDown()Returnstrueif and only if allEventExecutors managed by thisEventExecutorGroupare being shut down gracefully or was shut down.booleanisTerminated()Returnstrueif allEventExecutors managed by thisEventExecutorGroupare shut down, and all of their tasks have completed.protected Queue<Runnable>newTaskQueue(int maxPendingTasks)Create a newQueuewhich will holds the tasks to execute.protected booleanofferTask(Runnable task)intpendingTasks()Return the number of tasks that are pending for processing (excluding the scheduled tasks).protected RunnablepollTask()protected static voidreject()voidremoveShutdownHook(Runnable task)Remove a previous addedRunnableas a shutdown hookprotected booleanremoveTask(Runnable task)protected voidrun()Run tasks that are submitted to thisSingleThreadEventExecutor.protected intrunAllTasks(int maxTasks)Poll all tasks from the task queue and run them viaRunnable.run()method.Future<Void>shutdownGracefully(long quietPeriod, long timeout, TimeUnit unit)Signals this executor that the caller wants the executor to be shut down.protected RunnabletakeTask()Take the nextRunnablefrom the task queue and so will block if no task is currently present.Future<Void>terminationFuture()Returns theFuturewhich is notified when allEventExecutors managed by thisEventExecutorGrouphave been terminated.ThreadPropertiesthreadProperties()protected voidupdateLastExecutionTime()Updates the internal timestamp that tells when a submitted task was executed most recently.protected booleanwakesUpForTask(Runnable task)protected voidwakeup(boolean inEventLoop)-
Methods inherited from class io.netty5.util.concurrent.AbstractScheduledEventExecutor
cancelScheduledTasks, getCurrentTimeNanos, hasScheduledTasks, initialNanoTime, nanoTime, newRunnableScheduledFuture, newScheduledTaskFor, nextScheduledTaskNano, pollScheduledTask, pollScheduledTask, schedule, schedule, schedule, scheduleAtFixedRate, scheduleWithFixedDelay
-
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
inEventLoop, iterator, newFailedFuture, newPromise, newSucceededFuture, next, schedule, schedule, scheduleAtFixedRate, scheduleWithFixedDelay, submit, submit, submit
-
Methods inherited from interface io.netty5.util.concurrent.EventExecutorGroup
shutdownGracefully
-
Methods inherited from interface io.netty5.util.concurrent.FuturePromiseFactory
newSucceededFuture
-
Methods inherited from interface java.lang.Iterable
forEach, spliterator
-
-
-
-
Constructor Detail
-
SingleThreadEventExecutor
public SingleThreadEventExecutor()
Create a new instance
-
SingleThreadEventExecutor
public SingleThreadEventExecutor(ThreadFactory threadFactory)
Create a new instance- Parameters:
threadFactory- theThreadFactorywhich will be used for the usedThread
-
SingleThreadEventExecutor
public SingleThreadEventExecutor(ThreadFactory threadFactory, int maxPendingTasks, RejectedExecutionHandler rejectedHandler)
Create a new instance- Parameters:
threadFactory- theThreadFactorywhich will be used for the usedThreadmaxPendingTasks- the maximum number of pending tasks before new tasks will be rejected.rejectedHandler- theRejectedExecutionHandlerto use.
-
SingleThreadEventExecutor
public SingleThreadEventExecutor(Executor executor)
Create a new instance- Parameters:
executor- theExecutorwhich will be used for executing
-
SingleThreadEventExecutor
public SingleThreadEventExecutor(Executor executor, int maxPendingTasks, RejectedExecutionHandler rejectedHandler)
Create a new instance- Parameters:
executor- theExecutorwhich will be used for executingmaxPendingTasks- the maximum number of pending tasks before new tasks will be rejected.rejectedHandler- theRejectedExecutionHandlerto use.
-
-
Method Detail
-
newTaskQueue
protected Queue<Runnable> newTaskQueue(int maxPendingTasks)
Create a newQueuewhich will holds the tasks to execute. This default implementation will return aLinkedBlockingQueuebut if your sub-class ofSingleThreadEventExecutorwill not do any blocking calls on the thisQueueit may make sense to@Overridethis and return some more performant implementation that does not support blocking operations at all. Be aware that the implementation ofrun()depends on aBlockingQueueso you will need to overriderun()as well if you return a nonBlockingQueuefrom this method. As this method is called from within the constructor you can only use the parameters passed into the method when overriding this method.
-
interruptThread
protected final void interruptThread()
Interrupt the current runningThread.
-
pollTask
protected final Runnable pollTask()
-
takeTask
protected final Runnable takeTask()
Take the nextRunnablefrom the task queue and so will block if no task is currently present.Be aware that this method will throw an
This method must be called from theUnsupportedOperationExceptionif the task queue, which was created vianewTaskQueue(int), does not implementBlockingQueue.EventExecutorthread.- Returns:
nullif the executor thread has been interrupted or waken up.
-
hasTasks
protected final boolean hasTasks()
- See Also:
Collection.isEmpty()
-
pendingTasks
public final int pendingTasks()
Return the number of tasks that are pending for processing (excluding the scheduled tasks).
-
offerTask
protected final boolean offerTask(Runnable task)
- See Also:
Queue.offer(Object)
-
removeTask
protected final boolean removeTask(Runnable task)
- See Also:
Collection.remove(Object)
-
runAllTasks
protected int runAllTasks(int maxTasks)
Poll all tasks from the task queue and run them viaRunnable.run()method. This method must be called from theEventExecutorthread.- Returns:
- the number of processed tasks.
-
delayNanos
protected final long delayNanos(long currentTimeNanos)
Returns the amount of time left until the scheduled task with the closest dead line is executed. This method must be called from theEventExecutorthread.
-
deadlineNanos
protected final long deadlineNanos()
Returns the absolute point in time (relative toAbstractScheduledEventExecutor.getCurrentTimeNanos()()}) at which the next closest scheduled task should run or-1if none is scheduled at the mment. This method must be called from theEventExecutorthread.
-
updateLastExecutionTime
protected final void updateLastExecutionTime()
Updates the internal timestamp that tells when a submitted task was executed most recently.runAllTasks(int)updates this timestamp automatically, and thus there's usually no need to call this method. However, if you take the tasks manually usingtakeTask()orpollTask(), you have to call this method at the end of task execution loop if you execute a task for accurate quiet period checks. This method must be called from theEventExecutorthread.
-
run
protected void run()
Run tasks that are submitted to thisSingleThreadEventExecutor. The implementation depends on the fact thatnewTaskQueue(int)returns aBlockingQueue. If you change this by overridingnewTaskQueue(int)be aware that you also need to overriderun(). This method must be called from theEventExecutorthread.
-
cleanup
protected void cleanup()
Do nothing, sub-classes may override.
-
wakeup
protected void wakeup(boolean inEventLoop)
-
inEventLoop
public final boolean inEventLoop(Thread thread)
Description copied from interface:EventExecutor- Specified by:
inEventLoopin interfaceEventExecutor
-
addShutdownHook
public final void addShutdownHook(Runnable task)
Add aRunnablewhich will be executed on shutdown of this instance
-
removeShutdownHook
public final void removeShutdownHook(Runnable task)
Remove a previous addedRunnableas a shutdown hook
-
shutdownGracefully
public final Future<Void> shutdownGracefully(long quietPeriod, long timeout, TimeUnit unit)
Description copied from interface:EventExecutorGroupSignals this executor that the caller wants the executor to be shut down. Once this method is called,EventExecutorGroup.isShuttingDown()starts to returntrue, and the executor prepares to shut itself down. This method ensures that no tasks are submitted for 'the quiet period' (usually a couple seconds) before it shuts itself down. If a task is submitted during the quiet period, it is guaranteed to be accepted and the quiet period will start over.- Specified by:
shutdownGracefullyin interfaceEventExecutorGroup- Parameters:
quietPeriod- the quiet period as described in the documentationtimeout- the maximum amount of time to wait until the executor is shutting down regardless if a task was submitted during the quiet period.unit- the unit ofquietPeriodandtimeout- Returns:
- the
EventExecutorGroup.terminationFuture()
-
terminationFuture
public final Future<Void> terminationFuture()
Description copied from interface:EventExecutorGroupReturns theFuturewhich is notified when allEventExecutors managed by thisEventExecutorGrouphave been terminated.- Specified by:
terminationFuturein interfaceEventExecutorGroup- Returns:
- The
Futurerepresenting the termination of thisEventExecutorGroup.
-
isShuttingDown
public final boolean isShuttingDown()
Description copied from interface:EventExecutorGroupReturnstrueif and only if allEventExecutors managed by thisEventExecutorGroupare being shut down gracefully or was shut down.An executor group that "is shutting down" can still accept new tasks for a little while (the grace period), but will eventually start rejecting new tasks. At that point, the executor group will be shut down.
- Specified by:
isShuttingDownin interfaceEventExecutorGroup- Returns:
trueif all executors in this group have at least started shutting down, otherwisefalse.
-
isShutdown
public final boolean isShutdown()
Description copied from interface:EventExecutorGroupReturnstrueif allEventExecutors managed by thisEventExecutorGrouphave been shut down gracefully and moved past the grace period so that they are no longer accepting any new tasks.An executor group that "is shut down" might still be executing tasks that it has queued up, but it will no longer be accepting any new tasks. Once all running and queued tasks have completed, the executor group will be terminated.
- Specified by:
isShutdownin interfaceEventExecutorGroup- Returns:
trueif all executors in this group have shut down and are no longer accepting any new tasks.
-
isTerminated
public final boolean isTerminated()
Description copied from interface:EventExecutorGroupReturnstrueif allEventExecutors managed by thisEventExecutorGroupare shut down, and all of their tasks have completed.- Specified by:
isTerminatedin interfaceEventExecutorGroup- Returns:
trueif all executors in this group have terminated.
-
confirmShutdown
protected final boolean confirmShutdown()
Confirm that the shutdown if the instance should be done now! This method must be called from theEventExecutorthread.
-
awaitTermination
public final boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedExceptionDescription copied from interface:EventExecutorGroupWait for thisEventExecutorGroupto terminate, up to the given timeout.- Specified by:
awaitTerminationin interfaceEventExecutorGroup- Parameters:
timeout- The non-negative maximum amount of time to wait for the executor group to terminate.unit- The non-null time unit of the timeout.- Returns:
trueif the executor group terminated within the specific timeout.- Throws:
InterruptedException- If this thread was interrupted while waiting for executor group to terminate.
-
execute
public void execute(Runnable task)
- Specified by:
executein interfaceEventExecutor- Specified by:
executein interfaceEventExecutorGroup- Specified by:
executein interfaceExecutor
-
threadProperties
public final ThreadProperties threadProperties() throws InterruptedException
Returns theThreadPropertiesof theThreadthat powers theSingleThreadEventExecutor. If theSingleThreadEventExecutoris not started yet, this operation will start it and block until it is fully started.- Throws:
InterruptedException- if this thread is interrupted while waiting for the thread to start.
-
wakesUpForTask
protected boolean wakesUpForTask(Runnable task)
-
reject
protected static void reject()
-
-