public abstract class SingleThreadEventExecutor extends AbstractScheduledEventExecutor implements OrderedEventExecutor
OrderedEventExecutor's that execute all its submitted tasks in a single thread.| Modifier and Type | Class and Description |
|---|---|
protected static interface |
SingleThreadEventExecutor.NonWakeupRunnable
Deprecated.
override
wakesUpForTask(java.lang.Runnable) to re-create this behaviour |
AbstractEventExecutor.LazyRunnable| Modifier | Constructor and Description |
|---|---|
protected |
SingleThreadEventExecutor(EventExecutorGroup parent,
Executor executor,
boolean addTaskWakesUp)
Create a new instance
|
protected |
SingleThreadEventExecutor(EventExecutorGroup parent,
Executor executor,
boolean addTaskWakesUp,
int maxPendingTasks,
RejectedExecutionHandler rejectedHandler)
Create a new instance
|
protected |
SingleThreadEventExecutor(EventExecutorGroup parent,
Executor executor,
boolean addTaskWakesUp,
Queue<Runnable> taskQueue,
RejectedExecutionHandler rejectedHandler) |
protected |
SingleThreadEventExecutor(EventExecutorGroup parent,
ThreadFactory threadFactory,
boolean addTaskWakesUp)
Create a new instance
|
protected |
SingleThreadEventExecutor(EventExecutorGroup parent,
ThreadFactory threadFactory,
boolean addTaskWakesUp,
int maxPendingTasks,
RejectedExecutionHandler rejectedHandler)
Create a new instance
|
| Modifier and Type | Method and Description |
|---|---|
void |
addShutdownHook(Runnable task)
Add a
Runnable which will be executed on shutdown of this instance |
protected void |
addTask(Runnable task)
Add a task to the task queue, or throws a
RejectedExecutionException if this instance was shutdown
before. |
protected void |
afterRunningAllTasks()
Invoked before returning from
runAllTasks() and runAllTasks(long). |
boolean |
awaitTermination(long timeout,
TimeUnit unit) |
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 to
AbstractScheduledEventExecutor.getCurrentTimeNanos()) at which the next
closest scheduled task should run. |
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 running
Thread. |
<T> List<Future<T>> |
invokeAll(Collection<? extends Callable<T>> tasks) |
<T> List<Future<T>> |
invokeAll(Collection<? extends Callable<T>> tasks,
long timeout,
TimeUnit unit) |
<T> T |
invokeAny(Collection<? extends Callable<T>> tasks) |
<T> T |
invokeAny(Collection<? extends Callable<T>> tasks,
long timeout,
TimeUnit unit) |
boolean |
isShutdown() |
boolean |
isShuttingDown()
Returns
true if and only if all EventExecutors managed by this EventExecutorGroup
are being shut down gracefully or was shut down. |
boolean |
isTerminated() |
void |
lazyExecute(Runnable task)
Like
Executor.execute(Runnable) but does not guarantee the task will be run until either
a non-lazy task is executed or the executor is shut down. |
protected Queue<Runnable> |
newTaskQueue()
Deprecated.
Please use and override
newTaskQueue(int). |
protected Queue<Runnable> |
newTaskQueue(int maxPendingTasks)
Create a new
Queue which will holds the tasks to execute. |
protected Runnable |
peekTask() |
int |
pendingTasks()
Return the number of tasks that are pending for processing.
|
protected Runnable |
pollTask() |
protected static Runnable |
pollTaskFrom(Queue<Runnable> taskQueue) |
protected static void |
reject() |
protected void |
reject(Runnable task)
Offers the task to the associated
RejectedExecutionHandler. |
void |
removeShutdownHook(Runnable task)
Remove a previous added
Runnable as a shutdown hook |
protected boolean |
removeTask(Runnable task) |
protected abstract void |
run()
Run the tasks in the
taskQueue |
protected boolean |
runAllTasks()
Poll all tasks from the task queue and run them via
Runnable.run() method. |
protected boolean |
runAllTasks(long timeoutNanos)
Poll all tasks from the task queue and run them via
Runnable.run() method. |
protected boolean |
runAllTasksFrom(Queue<Runnable> taskQueue)
Runs all tasks from the passed
taskQueue. |
protected boolean |
runScheduledAndExecutorTasks(int maxDrainAttempts)
Execute all expired scheduled tasks and all current tasks in the executor queue until both queues are empty,
or
maxDrainAttempts has been exceeded. |
void |
shutdown()
Deprecated.
|
Future<?> |
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 next
Runnable from the task queue and so will block if no task is currently present. |
Future<?> |
terminationFuture()
Returns the
Future which is notified when all EventExecutors managed by this
EventExecutorGroup 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)
Can be overridden to control which tasks require waking the
EventExecutor thread
if it is waiting so that they can be run immediately. |
protected void |
wakeup(boolean inEventLoop) |
afterScheduledTaskSubmitted, beforeScheduledTaskSubmitted, cancelScheduledTasks, deadlineToDelayNanos, getCurrentTimeNanos, hasScheduledTasks, initialNanoTime, nanoTime, nextScheduledTaskDeadlineNanos, nextScheduledTaskNano, pollScheduledTask, pollScheduledTask, schedule, schedule, scheduleAtFixedRate, scheduleWithFixedDelay, validateScheduledinEventLoop, iterator, newFailedFuture, newProgressivePromise, newPromise, newSucceededFuture, newTaskFor, newTaskFor, next, parent, runTask, safeExecute, shutdownGracefully, shutdownNow, submit, submit, submitclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitinEventLoop, newFailedFuture, newProgressivePromise, newPromise, newSucceededFuture, next, parentiterator, schedule, schedule, scheduleAtFixedRate, scheduleWithFixedDelay, shutdownGracefully, shutdownNow, submit, submit, submitforEach, spliteratorprotected SingleThreadEventExecutor(EventExecutorGroup parent, ThreadFactory threadFactory, boolean addTaskWakesUp)
parent - the EventExecutorGroup which is the parent of this instance and belongs to itthreadFactory - the ThreadFactory which will be used for the used ThreadaddTaskWakesUp - true if and only if invocation of addTask(Runnable) will wake up the
executor threadprotected SingleThreadEventExecutor(EventExecutorGroup parent, ThreadFactory threadFactory, boolean addTaskWakesUp, int maxPendingTasks, RejectedExecutionHandler rejectedHandler)
parent - the EventExecutorGroup which is the parent of this instance and belongs to itthreadFactory - the ThreadFactory which will be used for the used ThreadaddTaskWakesUp - true if and only if invocation of addTask(Runnable) will wake up the
executor threadmaxPendingTasks - the maximum number of pending tasks before new tasks will be rejected.rejectedHandler - the RejectedExecutionHandler to use.protected SingleThreadEventExecutor(EventExecutorGroup parent, Executor executor, boolean addTaskWakesUp)
parent - the EventExecutorGroup which is the parent of this instance and belongs to itexecutor - the Executor which will be used for executingaddTaskWakesUp - true if and only if invocation of addTask(Runnable) will wake up the
executor threadprotected SingleThreadEventExecutor(EventExecutorGroup parent, Executor executor, boolean addTaskWakesUp, int maxPendingTasks, RejectedExecutionHandler rejectedHandler)
parent - the EventExecutorGroup which is the parent of this instance and belongs to itexecutor - the Executor which will be used for executingaddTaskWakesUp - true if and only if invocation of addTask(Runnable) will wake up the
executor threadmaxPendingTasks - the maximum number of pending tasks before new tasks will be rejected.rejectedHandler - the RejectedExecutionHandler to use.protected SingleThreadEventExecutor(EventExecutorGroup parent, Executor executor, boolean addTaskWakesUp, Queue<Runnable> taskQueue, RejectedExecutionHandler rejectedHandler)
@Deprecated protected Queue<Runnable> newTaskQueue()
newTaskQueue(int).protected Queue<Runnable> newTaskQueue(int maxPendingTasks)
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.protected void interruptThread()
Thread.protected Runnable pollTask()
Queue.poll()protected Runnable takeTask()
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(), does not implement BlockingQueue.
null if the executor thread has been interrupted or waken up.protected Runnable peekTask()
Queue.peek()protected boolean hasTasks()
Collection.isEmpty()public int pendingTasks()
protected void addTask(Runnable task)
RejectedExecutionException if this instance was shutdown
before.protected boolean removeTask(Runnable task)
Collection.remove(Object)protected boolean runAllTasks()
Runnable.run() method.true if and only if at least one task was runprotected final boolean runScheduledAndExecutorTasks(int maxDrainAttempts)
maxDrainAttempts has been exceeded.maxDrainAttempts - The maximum amount of times this method attempts to drain from queues. This is to prevent
continuous task execution and scheduling from preventing the EventExecutor thread to
make progress and return to the selector mechanism to process inbound I/O events.true if at least one task was run.protected final boolean runAllTasksFrom(Queue<Runnable> taskQueue)
taskQueue.taskQueue - To poll and execute all tasks.true if at least one task was executed.protected boolean runAllTasks(long timeoutNanos)
Runnable.run() method. This method stops running
the tasks in the task queue and returns if it ran longer than timeoutNanos.protected void afterRunningAllTasks()
runAllTasks() and runAllTasks(long).protected long delayNanos(long currentTimeNanos)
protected long deadlineNanos()
AbstractScheduledEventExecutor.getCurrentTimeNanos()) at which the next
closest scheduled task should run.protected void updateLastExecutionTime()
runAllTasks() and runAllTasks(long) 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 for accurate quiet period
checks.protected abstract void run()
taskQueueprotected void cleanup()
protected void wakeup(boolean inEventLoop)
public boolean inEventLoop(Thread thread)
EventExecutorinEventLoop in interface EventExecutorpublic void addShutdownHook(Runnable task)
Runnable which will be executed on shutdown of this instancepublic void removeShutdownHook(Runnable task)
Runnable as a shutdown hookpublic Future<?> shutdownGracefully(long quietPeriod, long timeout, TimeUnit unit)
EventExecutorGroupEventExecutorGroup.isShuttingDown() starts to return true, and the executor prepares to shut itself down.
Unlike EventExecutorGroup.shutdown(), graceful shutdown 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.shutdownGracefully in interface EventExecutorGroupquietPeriod - the quiet period as described in the documentationtimeout - the maximum amount of time to wait until the executor is EventExecutorGroup.shutdown()
regardless if a task was submitted during the quiet periodunit - the unit of quietPeriod and timeoutEventExecutorGroup.terminationFuture()public Future<?> terminationFuture()
EventExecutorGroupFuture which is notified when all EventExecutors managed by this
EventExecutorGroup have been terminated.terminationFuture in interface EventExecutorGroup@Deprecated public void shutdown()
shutdown in interface EventExecutorGroupshutdown in interface ExecutorServiceshutdown in class AbstractEventExecutorpublic boolean isShuttingDown()
EventExecutorGrouptrue if and only if all EventExecutors managed by this EventExecutorGroup
are being shut down gracefully or was shut down.isShuttingDown in interface EventExecutorGrouppublic boolean isShutdown()
isShutdown in interface ExecutorServicepublic boolean isTerminated()
isTerminated in interface ExecutorServiceprotected boolean confirmShutdown()
public boolean awaitTermination(long timeout,
TimeUnit unit)
throws InterruptedException
awaitTermination in interface ExecutorServiceInterruptedExceptionpublic void lazyExecute(Runnable task)
AbstractEventExecutorExecutor.execute(Runnable) but does not guarantee the task will be run until either
a non-lazy task is executed or the executor is shut down.
The default implementation just delegates to Executor.execute(Runnable).
lazyExecute in class AbstractEventExecutorpublic <T> T invokeAny(Collection<? extends Callable<T>> tasks) throws InterruptedException, ExecutionException
invokeAny in interface ExecutorServiceinvokeAny in class AbstractExecutorServiceInterruptedExceptionExecutionExceptionpublic <T> T invokeAny(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException
invokeAny in interface ExecutorServiceinvokeAny in class AbstractExecutorServiceInterruptedExceptionExecutionExceptionTimeoutExceptionpublic <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks) throws InterruptedException
invokeAll in interface ExecutorServiceinvokeAll in class AbstractExecutorServiceInterruptedExceptionpublic <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) throws InterruptedException
invokeAll in interface ExecutorServiceinvokeAll in class AbstractExecutorServiceInterruptedExceptionpublic final ThreadProperties threadProperties()
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.protected boolean wakesUpForTask(Runnable task)
EventExecutor thread
if it is waiting so that they can be run immediately.protected static void reject()
protected final void reject(Runnable task)
RejectedExecutionHandler.task - to reject.Copyright © 2008–2025 The Netty Project. All rights reserved.