- 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 int
DEFAULT_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 void
addShutdownHook(Runnable task)
Add aRunnable
which will be executed on shutdown of this instanceboolean
awaitTermination(long timeout, TimeUnit unit)
Wait for thisEventExecutorGroup
to terminate, up to the given timeout.protected void
cleanup()
Do nothing, sub-classes may override.protected boolean
confirmShutdown()
Confirm that the shutdown if the instance should be done now!protected long
deadlineNanos()
Returns the absolute point in time (relative toAbstractScheduledEventExecutor.getCurrentTimeNanos()
()}) at which the next closest scheduled task should run or-1
if none is scheduled at the mment.protected long
delayNanos(long currentTimeNanos)
Returns the amount of time left until the scheduled task with the closest dead line is executed.void
execute(Runnable task)
protected boolean
hasTasks()
boolean
inEventLoop(Thread thread)
protected void
interruptThread()
Interrupt the current runningThread
.boolean
isShutdown()
Returnstrue
if allEventExecutor
s managed by thisEventExecutorGroup
have been shut down gracefully and moved past the grace period so that they are no longer accepting any new tasks.boolean
isShuttingDown()
Returnstrue
if and only if allEventExecutor
s managed by thisEventExecutorGroup
are being shut down gracefully or was shut down.boolean
isTerminated()
Returnstrue
if allEventExecutor
s managed by thisEventExecutorGroup
are shut down, and all of their tasks have completed.protected Queue<Runnable>
newTaskQueue(int maxPendingTasks)
Create a newQueue
which will holds the tasks to execute.protected boolean
offerTask(Runnable task)
int
pendingTasks()
Return the number of tasks that are pending for processing (excluding the scheduled tasks).protected Runnable
pollTask()
protected static void
reject()
void
removeShutdownHook(Runnable task)
Remove a previous addedRunnable
as a shutdown hookprotected boolean
removeTask(Runnable task)
protected void
run()
Run tasks that are submitted to thisSingleThreadEventExecutor
.protected int
runAllTasks(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 Runnable
takeTask()
Take the nextRunnable
from the task queue and so will block if no task is currently present.Future<Void>
terminationFuture()
Returns theFuture
which is notified when allEventExecutor
s managed by thisEventExecutorGroup
have been terminated.ThreadProperties
threadProperties()
protected void
updateLastExecutionTime()
Updates the internal timestamp that tells when a submitted task was executed most recently.protected boolean
wakesUpForTask(Runnable task)
protected void
wakeup(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
- theThreadFactory
which will be used for the usedThread
-
SingleThreadEventExecutor
public SingleThreadEventExecutor(ThreadFactory threadFactory, int maxPendingTasks, RejectedExecutionHandler rejectedHandler)
Create a new instance- Parameters:
threadFactory
- theThreadFactory
which will be used for the usedThread
maxPendingTasks
- the maximum number of pending tasks before new tasks will be rejected.rejectedHandler
- theRejectedExecutionHandler
to use.
-
SingleThreadEventExecutor
public SingleThreadEventExecutor(Executor executor)
Create a new instance- Parameters:
executor
- theExecutor
which will be used for executing
-
SingleThreadEventExecutor
public SingleThreadEventExecutor(Executor executor, int maxPendingTasks, RejectedExecutionHandler rejectedHandler)
Create a new instance- Parameters:
executor
- theExecutor
which will be used for executingmaxPendingTasks
- the maximum number of pending tasks before new tasks will be rejected.rejectedHandler
- theRejectedExecutionHandler
to use.
-
-
Method Detail
-
newTaskQueue
protected Queue<Runnable> newTaskQueue(int maxPendingTasks)
Create a newQueue
which will holds the tasks to execute. This default implementation will return aLinkedBlockingQueue
but if your sub-class ofSingleThreadEventExecutor
will not do any blocking calls on the thisQueue
it may make sense to@Override
this and return some more performant implementation that does not support blocking operations at all. Be aware that the implementation ofrun()
depends on aBlockingQueue
so you will need to overriderun()
as well if you return a nonBlockingQueue
from 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 nextRunnable
from 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 theUnsupportedOperationException
if the task queue, which was created vianewTaskQueue(int)
, does not implementBlockingQueue
.EventExecutor
thread.- Returns:
null
if 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 theEventExecutor
thread.- 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 theEventExecutor
thread.
-
deadlineNanos
protected final long deadlineNanos()
Returns the absolute point in time (relative toAbstractScheduledEventExecutor.getCurrentTimeNanos()
()}) at which the next closest scheduled task should run or-1
if none is scheduled at the mment. This method must be called from theEventExecutor
thread.
-
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 theEventExecutor
thread.
-
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 theEventExecutor
thread.
-
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:
inEventLoop
in interfaceEventExecutor
-
addShutdownHook
public final void addShutdownHook(Runnable task)
Add aRunnable
which will be executed on shutdown of this instance
-
removeShutdownHook
public final void removeShutdownHook(Runnable task)
Remove a previous addedRunnable
as a shutdown hook
-
shutdownGracefully
public final Future<Void> shutdownGracefully(long quietPeriod, long timeout, TimeUnit unit)
Description copied from interface:EventExecutorGroup
Signals 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:
shutdownGracefully
in 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 ofquietPeriod
andtimeout
- Returns:
- the
EventExecutorGroup.terminationFuture()
-
terminationFuture
public final Future<Void> terminationFuture()
Description copied from interface:EventExecutorGroup
Returns theFuture
which is notified when allEventExecutor
s managed by thisEventExecutorGroup
have been terminated.- Specified by:
terminationFuture
in interfaceEventExecutorGroup
- Returns:
- The
Future
representing the termination of thisEventExecutorGroup
.
-
isShuttingDown
public final boolean isShuttingDown()
Description copied from interface:EventExecutorGroup
Returnstrue
if and only if allEventExecutor
s managed by thisEventExecutorGroup
are 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:
isShuttingDown
in interfaceEventExecutorGroup
- Returns:
true
if all executors in this group have at least started shutting down, otherwisefalse
.
-
isShutdown
public final boolean isShutdown()
Description copied from interface:EventExecutorGroup
Returnstrue
if allEventExecutor
s managed by thisEventExecutorGroup
have 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:
isShutdown
in interfaceEventExecutorGroup
- Returns:
true
if 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:EventExecutorGroup
Returnstrue
if allEventExecutor
s managed by thisEventExecutorGroup
are shut down, and all of their tasks have completed.- Specified by:
isTerminated
in interfaceEventExecutorGroup
- Returns:
true
if 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 theEventExecutor
thread.
-
awaitTermination
public final boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException
Description copied from interface:EventExecutorGroup
Wait for thisEventExecutorGroup
to terminate, up to the given timeout.- Specified by:
awaitTermination
in 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:
true
if 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:
execute
in interfaceEventExecutor
- Specified by:
execute
in interfaceEventExecutorGroup
- Specified by:
execute
in interfaceExecutor
-
threadProperties
public final ThreadProperties threadProperties() throws InterruptedException
Returns theThreadProperties
of theThread
that powers theSingleThreadEventExecutor
. If theSingleThreadEventExecutor
is 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()
-
-