Top Description Inners Fields Constructors Methods
io.netty5.util.concurrent

public Class SingleThreadEventExecutor

extends AbstractScheduledEventExecutor
implements OrderedEventExecutor
Class Inheritance
All Implemented Interfaces
io.netty5.util.concurrent.OrderedEventExecutor, io.netty5.util.concurrent.EventExecutor, io.netty5.util.concurrent.FuturePromiseFactory, io.netty5.util.concurrent.EventExecutorGroup, java.util.concurrent.Executor, java.lang.Iterable
Imports
io.netty5.util.internal.SystemPropertyUtil, .ThreadExecutorMap, io.netty5.util.internal.logging.InternalLogger, .InternalLoggerFactory, org.jetbrains.annotations.Async.Execute, .Async.Schedule, java.lang.Thread.State, java.util.ArrayList, .LinkedHashSet, .List, .Queue, .Set, java.util.concurrent.BlockingQueue, .CountDownLatch, .Executor, .LinkedBlockingQueue, .RejectedExecutionException, .ThreadFactory, .TimeUnit, java.util.concurrent.atomic.AtomicIntegerFieldUpdater, .AtomicReferenceFieldUpdater

OrderedEventExecutor's implementation that execute all its submitted tasks in a single thread.

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class

Field Summary

Modifier and TypeField and Description
private final boolean
protected static final int
private final Executor
private volatile long
private long
private volatile long
private volatile boolean
private long
private static final InternalLogger
private static final Runnable
private static final AtomicReferenceFieldUpdater<SingleThreadEventExecutor, ThreadProperties>
private final RejectedExecutionHandler
private static final long
private final Set<Runnable>
private static final int
private static final int
private static final int
private static final int
private static final int
private volatile int
private static final AtomicIntegerFieldUpdater<SingleThreadEventExecutor>
private final Queue<Runnable>
private final Promise<Void>
private volatile Thread
private final CountDownLatch
private volatile ThreadProperties
private static final Runnable
Inherited from io.netty5.util.concurrent.AbstractScheduledEventExecutor:
START_TIME

Constructor Summary

AccessConstructor and Description
public
SingleThreadEventExecutor()

Create a new instance

public
SingleThreadEventExecutor(ThreadFactory
the ThreadFactory which will be used for the used Thread
threadFactory
)

Create a new instance

public
SingleThreadEventExecutor(ThreadFactory
the ThreadFactory which will be used for the used Thread
threadFactory
,
int
the maximum number of pending tasks before new tasks will be rejected.
maxPendingTasks
,
RejectedExecutionHandler rejectedHandler)

Create a new instance

public
SingleThreadEventExecutor(Executor
the Executor which will be used for executing
executor
)

Create a new instance

public
SingleThreadEventExecutor(Executor
the Executor which will be used for executing
executor
,
int
the maximum number of pending tasks before new tasks will be rejected.
maxPendingTasks
,
RejectedExecutionHandler rejectedHandler)

Create a new instance

Method Summary

Modifier and TypeMethod and Description
public final void
addShutdownHook(final Runnable task)

Add a Runnable which will be executed on shutdown of this instance

private void
addTask(Runnable task)

Add a task to the task queue, or throws a RejectedExecutionException if this instance was shutdown before.

public final boolean
awaitTermination(long
The non-negative maximum amount of time to wait for the executor group to terminate.
timeout
,
TimeUnit
The non-null time unit of the timeout.
unit
)

Overrides default io.netty5.util.concurrent.EventExecutorGroup.awaitTermination.

Wait for this EventExecutorGroup to terminate, up to the given timeout.
protected void
cleanup()

Do nothing, sub-classes may override.

protected final boolean
confirmShutdown()

Confirm that the shutdown if the instance should be done now!

pack-priv boolean
protected final long
deadlineNanos()

Returns the absolute point in time (relative to getCurrentTimeNanos() ()}) at which the next closest scheduled task should run or -1 if none is scheduled at the mment.

protected final long
delayNanos(long currentTimeNanos)

Returns the amount of time left until the scheduled task with the closest dead line is executed.

private void
pack-priv final int
private boolean
ensureThreadStarted(int oldState)

public void
execute(Runnable
the runnable task
task
)

Implements io.netty5.util.concurrent.EventExecutor.execute.

Executes the given command at some time in the future.
private boolean
protected final boolean
public final boolean
inEventLoop(Thread thread)

Implements io.netty5.util.concurrent.EventExecutor.inEventLoop.

Return true if the given Thread is executed in the event loop, false otherwise.
protected final void
interruptThread()

Interrupt the current running Thread.

public final boolean
isShutdown()

Implements io.netty5.util.concurrent.EventExecutorGroup.isShutdown.

Returns true if all EventExecutors managed by this EventExecutorGroup have been shut down gracefully and moved past the grace period so that they are no longer accepting any new tasks.
public final boolean
public final boolean
isTerminated()

Overrides default io.netty5.util.concurrent.EventExecutorGroup.isTerminated.

Returns true if all EventExecutors managed by this EventExecutorGroup are shut down, and all of their tasks have completed.
protected Queue<Runnable>
newTaskQueue(int maxPendingTasks)

Create a new Queue which will holds the tasks to execute.

protected final boolean
public final int
pendingTasks()

Return the number of tasks that are pending for processing (excluding the scheduled tasks).

protected final Runnable
protected static void
public final void
removeShutdownHook(final Runnable task)

Remove a previous added Runnable as a shutdown hook

protected final boolean
protected void
run()

Run tasks that are submitted to this SingleThreadEventExecutor.

private boolean

Returns:

true if and only if at least one task was run
runAllTasks
()

Poll all tasks from the task queue and run them via Runnable#run() method.

protected int

Returns:

the number of processed tasks.
runAllTasks
(int maxTasks)

Poll all tasks from the task queue and run them via Runnable#run() method.

private boolean
private void
public final Future<Void>
shutdownGracefully(long
the quiet period as described in the documentation
quietPeriod
,
long
the maximum amount of time to wait until the executor is shutting down regardless if a task was submitted during the quiet period.
timeout
,
TimeUnit
the unit of quietPeriod and timeout
unit
)

Implements io.netty5.util.concurrent.EventExecutorGroup.shutdownGracefully.

Signals this executor that the caller wants the executor to be shut down.
private void
protected final Runnable

Returns:

null if the executor thread has been interrupted or waken up.
takeTask
()

Take the next Runnable from the task queue and so will block if no task is currently present.

public final Future<Void>
terminationFuture()

Implements io.netty5.util.concurrent.EventExecutorGroup.terminationFuture.

Returns the Future which is notified when all EventExecutors managed by this EventExecutorGroup have been terminated.
public final ThreadProperties
protected final void
updateLastExecutionTime()

Updates the internal timestamp that tells when a submitted task was executed most recently.

protected boolean
wakesUpForTask(Runnable task)

Returns true if wakeup(boolean) should be called for this Runnable, false otherwise.

protected void
wakeup(boolean inEventLoop)

Inherited from io.netty5.util.concurrent.AbstractScheduledEventExecutor:
cancelScheduledTasksdeadlineNanosdefaultCurrentTimeNanosgetCurrentTimeNanoshasScheduledTasksinitialNanoTimenanoTimenewRunnableScheduledFuturenewScheduledTaskFornextScheduledTaskNanopeekScheduledTaskpollScheduledTaskpollScheduledTaskremoveScheduledschedulescheduleschedulescheduleAtFixedRatescheduledTaskQueuescheduleWithFixedDelay

Field Detail

addTaskWakesUpback to summary
private final boolean addTaskWakesUp
DEFAULT_MAX_PENDING_EXECUTOR_TASKSback to summary
protected static final int DEFAULT_MAX_PENDING_EXECUTOR_TASKS
executorback to summary
private final Executor executor
gracefulShutdownQuietPeriodback to summary
private volatile long gracefulShutdownQuietPeriod
gracefulShutdownStartTimeback to summary
private long gracefulShutdownStartTime
gracefulShutdownTimeoutback to summary
private volatile long gracefulShutdownTimeout
interruptedback to summary
private volatile boolean interrupted
lastExecutionTimeback to summary
private long lastExecutionTime
loggerback to summary
private static final InternalLogger logger

Hides io.netty5.util.concurrent.AbstractEventExecutor.logger.

NOOP_TASKback to summary
private static final Runnable NOOP_TASK
PROPERTIES_UPDATERback to summary
private static final AtomicReferenceFieldUpdater<SingleThreadEventExecutor, ThreadProperties> PROPERTIES_UPDATER
rejectedExecutionHandlerback to summary
private final RejectedExecutionHandler rejectedExecutionHandler
SCHEDULE_PURGE_INTERVALback to summary
private static final long SCHEDULE_PURGE_INTERVAL
shutdownHooksback to summary
private final Set<Runnable> shutdownHooks
ST_NOT_STARTEDback to summary
private static final int ST_NOT_STARTED
ST_SHUTDOWNback to summary
private static final int ST_SHUTDOWN
ST_SHUTTING_DOWNback to summary
private static final int ST_SHUTTING_DOWN
ST_STARTEDback to summary
private static final int ST_STARTED
ST_TERMINATEDback to summary
private static final int ST_TERMINATED
stateback to summary
private volatile int state
Annotations
@SuppressWarnings:FieldMayBeFinal, unused
STATE_UPDATERback to summary
private static final AtomicIntegerFieldUpdater<SingleThreadEventExecutor> STATE_UPDATER
taskQueueback to summary
private final Queue<Runnable> taskQueue
terminationFutureback to summary
private final Promise<Void> terminationFuture
threadback to summary
private volatile Thread thread
threadLockback to summary
private final CountDownLatch threadLock
threadPropertiesback to summary
private volatile ThreadProperties threadProperties
Annotations
@SuppressWarnings:unused
WAKEUP_TASKback to summary
private static final Runnable WAKEUP_TASK

Constructor Detail

SingleThreadEventExecutorback to summary
public SingleThreadEventExecutor()

Create a new instance

SingleThreadEventExecutorback to summary
public SingleThreadEventExecutor(ThreadFactory threadFactory)

Create a new instance

Parameters
threadFactory:ThreadFactory

the ThreadFactory which will be used for the used Thread

SingleThreadEventExecutorback to summary
public SingleThreadEventExecutor(ThreadFactory threadFactory, int maxPendingTasks, RejectedExecutionHandler rejectedHandler)

Create a new instance

Parameters
threadFactory:ThreadFactory

the ThreadFactory which will be used for the used Thread

maxPendingTasks:int

the maximum number of pending tasks before new tasks will be rejected.

rejectedHandler:RejectedExecutionHandler

the RejectedExecutionHandler to use.

SingleThreadEventExecutorback to summary
public SingleThreadEventExecutor(Executor executor)

Create a new instance

Parameters
executor:Executor

the Executor which will be used for executing

SingleThreadEventExecutorback to summary
public SingleThreadEventExecutor(Executor executor, int maxPendingTasks, RejectedExecutionHandler rejectedHandler)

Create a new instance

Parameters
executor:Executor

the Executor which will be used for executing

maxPendingTasks:int

the maximum number of pending tasks before new tasks will be rejected.

rejectedHandler:RejectedExecutionHandler

the RejectedExecutionHandler to use.

Method Detail

addShutdownHookback to summary
public final void addShutdownHook(final Runnable task)

Add a Runnable which will be executed on shutdown of this instance

addTaskback to summary
private void addTask(Runnable task)

Add a task to the task queue, or throws a RejectedExecutionException if this instance was shutdown before.

awaitTerminationback to summary
public final boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException

Overrides default io.netty5.util.concurrent.EventExecutorGroup.awaitTermination.

Doc from io.netty5.util.concurrent.EventExecutorGroup.awaitTermination.

Wait for this EventExecutorGroup to terminate, up to the given timeout.

Parameters
timeout:long

The non-negative maximum amount of time to wait for the executor group to terminate.

unit:TimeUnit

The non-null time unit of the timeout.

Returns:boolean

true if the executor group terminated within the specific timeout.

Annotations
@Override
Exceptions
InterruptedException:
If this thread was interrupted while waiting for executor group to terminate.
cleanupback to summary
protected void cleanup()

Do nothing, sub-classes may override.

confirmShutdownback to summary
protected final boolean confirmShutdown()

Confirm that the shutdown if the instance should be done now! This method must be called from the EventExecutor thread.

confirmShutdown0back to summary
pack-priv boolean confirmShutdown0()
deadlineNanosback to summary
protected final long deadlineNanos()

Returns the absolute point in time (relative to 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 the EventExecutor thread.

delayNanosback to summary
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 the EventExecutor thread.

doStartThreadback to summary
private void doStartThread()
drainTasksback to summary
pack-priv final int drainTasks()
ensureThreadStartedback to summary
private boolean ensureThreadStarted(int oldState)
executeback to summary
public void execute(Runnable task)

Implements io.netty5.util.concurrent.EventExecutor.execute.

Doc from java.util.concurrent.Executor.execute.

Executes the given command at some time in the future. The command may execute in a new thread, in a pooled thread, or in the calling thread, at the discretion of the Executor implementation.

Parameters
task:Runnable

the runnable task

Annotations
@Override
fetchFromScheduledTaskQueueback to summary
private boolean fetchFromScheduledTaskQueue()
hasTasksback to summary
protected final boolean hasTasks()
See Also
Queue#isEmpty()
inEventLoopback to summary
public final boolean inEventLoop(Thread thread)

Implements io.netty5.util.concurrent.EventExecutor.inEventLoop.

Doc from io.netty5.util.concurrent.EventExecutor.inEventLoop.

Return true if the given Thread is executed in the event loop, false otherwise.

Annotations
@Override
interruptThreadback to summary
protected final void interruptThread()

Interrupt the current running Thread.

isShutdownback to summary
public final boolean isShutdown()

Implements io.netty5.util.concurrent.EventExecutorGroup.isShutdown.

Doc from io.netty5.util.concurrent.EventExecutorGroup.isShutdown.

Returns true if all EventExecutors managed by this EventExecutorGroup 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.

Returns:boolean

true if all executors in this group have shut down and are no longer accepting any new tasks.

Annotations
@Override
isShuttingDownback to summary
public final boolean isShuttingDown()

Implements io.netty5.util.concurrent.EventExecutorGroup.isShuttingDown.

Doc from io.netty5.util.concurrent.EventExecutorGroup.isShuttingDown.

Returns true if and only if all EventExecutors managed by this EventExecutorGroup 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.

Returns:boolean

true if all executors in this group have at least started shutting down, otherwise false.

Annotations
@Override
isTerminatedback to summary
public final boolean isTerminated()

Overrides default io.netty5.util.concurrent.EventExecutorGroup.isTerminated.

Doc from io.netty5.util.concurrent.EventExecutorGroup.isTerminated.

Returns true if all EventExecutors managed by this EventExecutorGroup are shut down, and all of their tasks have completed.

Returns:boolean

true if all executors in this group have terminated.

Annotations
@Override
newTaskQueueback to summary
protected Queue<Runnable> newTaskQueue(int maxPendingTasks)

Create a new Queue which will holds the tasks to execute. This default implementation will return a LinkedBlockingQueue but if your sub-class of SingleThreadEventExecutor will not do any blocking calls on the this Queue 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 of run() depends on a BlockingQueue so you will need to override run() as well if you return a non BlockingQueue 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.

offerTaskback to summary
protected final boolean offerTask(Runnable task)
See Also
Queue#offer(Object)
pendingTasksback to summary
public final int pendingTasks()

Return the number of tasks that are pending for processing (excluding the scheduled tasks).

pollTaskback to summary
protected final Runnable pollTask()
See Also
This method must be called from the <a href="io.netty5.util.concurrent.EventExecutor.html" class="jlink"><code class="coderef">EventExecutor</code></a> thread.
rejectback to summary
protected static void reject()
removeShutdownHookback to summary
public final void removeShutdownHook(final Runnable task)

Remove a previous added Runnable as a shutdown hook

removeTaskback to summary
protected final boolean removeTask(Runnable task)
See Also
Queue#remove(Object)
runback to summary
protected void run()

Run tasks that are submitted to this SingleThreadEventExecutor. The implementation depends on the fact that newTaskQueue(int) returns a BlockingQueue. If you change this by overriding newTaskQueue(int) be aware that you also need to override run(). This method must be called from the EventExecutor thread.

runAllTasksback to summary
private boolean runAllTasks()

Poll all tasks from the task queue and run them via Runnable#run() method. This method must be called from the EventExecutor thread.

Returns:boolean

true if and only if at least one task was run

runAllTasksback to summary
protected int runAllTasks(int maxTasks)

Poll all tasks from the task queue and run them via Runnable#run() method. This method must be called from the EventExecutor thread.

Returns:int

the number of processed tasks.

runShutdownHooksback to summary
private boolean runShutdownHooks()
runTaskback to summary
private void runTask(Runnable task)
shutdownGracefullyback to summary
public final Future<Void> shutdownGracefully(long quietPeriod, long timeout, TimeUnit unit)

Implements io.netty5.util.concurrent.EventExecutorGroup.shutdownGracefully.

Doc from io.netty5.util.concurrent.EventExecutorGroup.shutdownGracefully.

Signals this executor that the caller wants the executor to be shut down. Once this method is called, isShuttingDown() starts to return true, 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.

Parameters
quietPeriod:long

the quiet period as described in the documentation

timeout:long

the maximum amount of time to wait until the executor is shutting down regardless if a task was submitted during the quiet period.

unit:TimeUnit

the unit of quietPeriod and timeout

Returns:Future<Void>

the terminationFuture()

Annotations
@Override
startThreadback to summary
private void startThread()
takeTaskback to summary
protected final Runnable takeTask()

Take the next Runnable from the task queue and so will block if no task is currently present.

Be aware that this method will throw an UnsupportedOperationException if the task queue, which was created via newTaskQueue(int), does not implement BlockingQueue.

This method must be called from the EventExecutor thread.
Returns:Runnable

null if the executor thread has been interrupted or waken up.

terminationFutureback to summary
public final Future<Void> terminationFuture()

Implements io.netty5.util.concurrent.EventExecutorGroup.terminationFuture.

Doc from io.netty5.util.concurrent.EventExecutorGroup.terminationFuture.

Returns the Future which is notified when all EventExecutors managed by this EventExecutorGroup have been terminated.

Returns:Future<Void>

The Future representing the termination of this EventExecutorGroup.

Annotations
@Override
threadPropertiesback to summary
public final ThreadProperties threadProperties() throws InterruptedException

Returns the ThreadProperties of the Thread that powers the SingleThreadEventExecutor. If the SingleThreadEventExecutor is not started yet, this operation will start it and block until it is fully started.

Exceptions
InterruptedException:
if this thread is interrupted while waiting for the thread to start.
updateLastExecutionTimeback to summary
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 using takeTask() or pollTask(), 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 the EventExecutor thread.

wakesUpForTaskback to summary
protected boolean wakesUpForTask(Runnable task)

Returns true if wakeup(boolean) should be called for this Runnable, false otherwise.

wakeupback to summary
protected void wakeup(boolean inEventLoop)
io.netty5.util.concurrent back to summary

private final Class SingleThreadEventExecutor.DefaultThreadProperties

extends Object
implements ThreadProperties
Class Inheritance
  • java.lang.Object
  • io.netty5.util.concurrent.SingleThreadEventExecutor.DefaultThreadProperties
All Implemented Interfaces
io.netty5.util.concurrent.ThreadProperties

Field Summary

Modifier and TypeField and Description
private final Thread
t

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
public long
public boolean
public boolean
public boolean
public String
public int
public StackTraceElement[]
public Thread.State
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

tback to summary
private final Thread t

Constructor Detail

DefaultThreadPropertiesback to summary
pack-priv DefaultThreadProperties(Thread t)

Method Detail

idback to summary
public long id()

Implements io.netty5.util.concurrent.ThreadProperties.id.

Annotations
@Override
isAliveback to summary
public boolean isAlive()

Implements io.netty5.util.concurrent.ThreadProperties.isAlive.

Annotations
@Override
isDaemonback to summary
public boolean isDaemon()

Implements io.netty5.util.concurrent.ThreadProperties.isDaemon.

Annotations
@Override
isInterruptedback to summary
public boolean isInterrupted()

Implements io.netty5.util.concurrent.ThreadProperties.isInterrupted.

Annotations
@Override
nameback to summary
public String name()

Implements io.netty5.util.concurrent.ThreadProperties.name.

Annotations
@Override
priorityback to summary
public int priority()

Implements io.netty5.util.concurrent.ThreadProperties.priority.

Annotations
@Override
stackTraceback to summary
public StackTraceElement[] stackTrace()

Implements io.netty5.util.concurrent.ThreadProperties.stackTrace.

Annotations
@Override
stateback to summary
public Thread.State state()

Implements io.netty5.util.concurrent.ThreadProperties.state.

Annotations
@Override