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 |
|---|---|
private static class |
SingleThreadEventExecutor.DefaultThreadProperties |
| Modifier and Type | Field and Description |
|---|---|
private boolean |
addTaskWakesUp |
(package private) static int |
DEFAULT_MAX_PENDING_EXECUTOR_TASKS |
private java.util.concurrent.Executor |
executor |
private long |
gracefulShutdownQuietPeriod |
private long |
gracefulShutdownStartTime |
private long |
gracefulShutdownTimeout |
private boolean |
interrupted |
private long |
lastExecutionTime |
private static InternalLogger |
logger |
private int |
maxPendingTasks |
private static java.lang.Runnable |
NOOP_TASK |
private static java.util.concurrent.atomic.AtomicReferenceFieldUpdater<SingleThreadEventExecutor,ThreadProperties> |
PROPERTIES_UPDATER |
private RejectedExecutionHandler |
rejectedExecutionHandler |
private static long |
SCHEDULE_PURGE_INTERVAL |
private java.util.Set<java.lang.Runnable> |
shutdownHooks |
private static int |
ST_NOT_STARTED |
private static int |
ST_SHUTDOWN |
private static int |
ST_SHUTTING_DOWN |
private static int |
ST_STARTED |
private static int |
ST_TERMINATED |
private int |
state |
private static java.util.concurrent.atomic.AtomicIntegerFieldUpdater<SingleThreadEventExecutor> |
STATE_UPDATER |
private java.util.Queue<java.lang.Runnable> |
taskQueue |
private Promise<?> |
terminationFuture |
private java.lang.Thread |
thread |
private java.util.concurrent.Semaphore |
threadLock |
private ThreadProperties |
threadProperties |
private static java.lang.Runnable |
WAKEUP_TASK |
scheduledTaskQueueDEFAULT_SHUTDOWN_QUIET_PERIOD, DEFAULT_SHUTDOWN_TIMEOUT| Modifier | Constructor and Description |
|---|---|
protected |
SingleThreadEventExecutor(EventExecutorGroup parent,
java.util.concurrent.Executor executor,
boolean addTaskWakesUp)
Create a new instance
|
protected |
SingleThreadEventExecutor(EventExecutorGroup parent,
java.util.concurrent.Executor executor,
boolean addTaskWakesUp,
int maxPendingTasks,
RejectedExecutionHandler rejectedHandler)
Create a new instance
|
protected |
SingleThreadEventExecutor(EventExecutorGroup parent,
java.util.concurrent.ThreadFactory threadFactory,
boolean addTaskWakesUp)
Create a new instance
|
protected |
SingleThreadEventExecutor(EventExecutorGroup parent,
java.util.concurrent.ThreadFactory threadFactory,
boolean addTaskWakesUp,
int maxPendingTasks,
RejectedExecutionHandler rejectedHandler)
Create a new instance
|
| Modifier and Type | Method and Description |
|---|---|
void |
addShutdownHook(java.lang.Runnable task)
Add a
Runnable which will be executed on shutdown of this instance |
protected void |
addTask(java.lang.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,
java.util.concurrent.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 |
delayNanos(long currentTimeNanos)
Returns the amount of time left until the scheduled task with the closest dead line is executed.
|
private void |
doStartThread() |
void |
execute(java.lang.Runnable task) |
private boolean |
fetchFromScheduledTaskQueue() |
protected boolean |
hasTasks() |
boolean |
inEventLoop(java.lang.Thread thread)
Return
true if the given Thread is executed in the event loop,
false otherwise. |
protected void |
interruptThread()
Interrupt the current running
Thread. |
<T> java.util.List<java.util.concurrent.Future<T>> |
invokeAll(java.util.Collection<? extends java.util.concurrent.Callable<T>> tasks) |
<T> java.util.List<java.util.concurrent.Future<T>> |
invokeAll(java.util.Collection<? extends java.util.concurrent.Callable<T>> tasks,
long timeout,
java.util.concurrent.TimeUnit unit) |
<T> T |
invokeAny(java.util.Collection<? extends java.util.concurrent.Callable<T>> tasks) |
<T> T |
invokeAny(java.util.Collection<? extends java.util.concurrent.Callable<T>> tasks,
long timeout,
java.util.concurrent.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() |
protected java.util.Queue<java.lang.Runnable> |
newTaskQueue()
Deprecated.
Please use and override
newTaskQueue(int). |
protected java.util.Queue<java.lang.Runnable> |
newTaskQueue(int maxPendingTasks)
Create a new
Queue which will holds the tasks to execute. |
(package private) boolean |
offerTask(java.lang.Runnable task) |
protected java.lang.Runnable |
peekTask() |
int |
pendingTasks()
Return the number of tasks that are pending for processing.
|
protected java.lang.Runnable |
pollTask() |
protected static java.lang.Runnable |
pollTaskFrom(java.util.Queue<java.lang.Runnable> taskQueue) |
protected static void |
reject() |
protected void |
reject(java.lang.Runnable task)
Offers the task to the associated
RejectedExecutionHandler. |
void |
removeShutdownHook(java.lang.Runnable task)
Remove a previous added
Runnable as a shutdown hook |
protected boolean |
removeTask(java.lang.Runnable task) |
protected abstract void |
run() |
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(java.util.Queue<java.lang.Runnable> taskQueue)
Runs all tasks from the passed
taskQueue. |
private boolean |
runShutdownHooks() |
void |
shutdown()
Deprecated.
|
Future<?> |
shutdownGracefully(long quietPeriod,
long timeout,
java.util.concurrent.TimeUnit unit)
Signals this executor that the caller wants the executor to be shut down.
|
private void |
startThread() |
protected java.lang.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()
|
private void |
throwIfInEventLoop(java.lang.String method) |
protected void |
updateLastExecutionTime()
Updates the internal timestamp that tells when a submitted task was executed most recently.
|
protected boolean |
wakesUpForTask(java.lang.Runnable task) |
protected void |
wakeup(boolean inEventLoop) |
cancelScheduledTasks, hasScheduledTasks, nanoTime, nextScheduledTaskNano, peekScheduledTask, pollScheduledTask, pollScheduledTask, removeScheduled, schedule, schedule, schedule, scheduleAtFixedRate, scheduledTaskQueue, scheduleWithFixedDelayinEventLoop, iterator, newFailedFuture, newProgressivePromise, newPromise, newSucceededFuture, newTaskFor, newTaskFor, next, parent, 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, submitstatic final int DEFAULT_MAX_PENDING_EXECUTOR_TASKS
private static final InternalLogger logger
private static final int ST_NOT_STARTED
private static final int ST_STARTED
private static final int ST_SHUTTING_DOWN
private static final int ST_SHUTDOWN
private static final int ST_TERMINATED
private static final java.lang.Runnable WAKEUP_TASK
private static final java.lang.Runnable NOOP_TASK
private static final java.util.concurrent.atomic.AtomicIntegerFieldUpdater<SingleThreadEventExecutor> STATE_UPDATER
private static final java.util.concurrent.atomic.AtomicReferenceFieldUpdater<SingleThreadEventExecutor,ThreadProperties> PROPERTIES_UPDATER
private final java.util.Queue<java.lang.Runnable> taskQueue
private volatile java.lang.Thread thread
private volatile ThreadProperties threadProperties
private final java.util.concurrent.Executor executor
private volatile boolean interrupted
private final java.util.concurrent.Semaphore threadLock
private final java.util.Set<java.lang.Runnable> shutdownHooks
private final boolean addTaskWakesUp
private final int maxPendingTasks
private final RejectedExecutionHandler rejectedExecutionHandler
private long lastExecutionTime
private volatile int state
private volatile long gracefulShutdownQuietPeriod
private volatile long gracefulShutdownTimeout
private long gracefulShutdownStartTime
private final Promise<?> terminationFuture
private static final long SCHEDULE_PURGE_INTERVAL
protected SingleThreadEventExecutor(EventExecutorGroup parent, java.util.concurrent.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, java.util.concurrent.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, java.util.concurrent.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, java.util.concurrent.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.@Deprecated protected java.util.Queue<java.lang.Runnable> newTaskQueue()
newTaskQueue(int).protected java.util.Queue<java.lang.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 java.lang.Runnable pollTask()
Queue.poll()protected static java.lang.Runnable pollTaskFrom(java.util.Queue<java.lang.Runnable> taskQueue)
protected java.lang.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.private boolean fetchFromScheduledTaskQueue()
protected java.lang.Runnable peekTask()
Queue.peek()protected boolean hasTasks()
Collection.isEmpty()public int pendingTasks()
protected void addTask(java.lang.Runnable task)
RejectedExecutionException if this instance was shutdown
before.final boolean offerTask(java.lang.Runnable task)
protected boolean removeTask(java.lang.Runnable task)
Collection.remove(Object)protected boolean runAllTasks()
Runnable.run() method.true if and only if at least one task was runprotected final boolean runAllTasksFrom(java.util.Queue<java.lang.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.@UnstableApi protected void afterRunningAllTasks()
runAllTasks() and runAllTasks(long).protected long delayNanos(long currentTimeNanos)
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()
protected void cleanup()
protected void wakeup(boolean inEventLoop)
public boolean inEventLoop(java.lang.Thread thread)
EventExecutortrue if the given Thread is executed in the event loop,
false otherwise.inEventLoop in interface EventExecutorpublic void addShutdownHook(java.lang.Runnable task)
Runnable which will be executed on shutdown of this instancepublic void removeShutdownHook(java.lang.Runnable task)
Runnable as a shutdown hookprivate boolean runShutdownHooks()
public Future<?> shutdownGracefully(long quietPeriod, long timeout, java.util.concurrent.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 java.util.concurrent.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 java.util.concurrent.ExecutorServicepublic boolean isTerminated()
isTerminated in interface java.util.concurrent.ExecutorServiceprotected boolean confirmShutdown()
public boolean awaitTermination(long timeout,
java.util.concurrent.TimeUnit unit)
throws java.lang.InterruptedException
awaitTermination in interface java.util.concurrent.ExecutorServicejava.lang.InterruptedExceptionpublic void execute(java.lang.Runnable task)
execute in interface java.util.concurrent.Executorpublic <T> T invokeAny(java.util.Collection<? extends java.util.concurrent.Callable<T>> tasks)
throws java.lang.InterruptedException,
java.util.concurrent.ExecutionException
invokeAny in interface java.util.concurrent.ExecutorServiceinvokeAny in class java.util.concurrent.AbstractExecutorServicejava.lang.InterruptedExceptionjava.util.concurrent.ExecutionExceptionpublic <T> T invokeAny(java.util.Collection<? extends java.util.concurrent.Callable<T>> tasks,
long timeout,
java.util.concurrent.TimeUnit unit)
throws java.lang.InterruptedException,
java.util.concurrent.ExecutionException,
java.util.concurrent.TimeoutException
invokeAny in interface java.util.concurrent.ExecutorServiceinvokeAny in class java.util.concurrent.AbstractExecutorServicejava.lang.InterruptedExceptionjava.util.concurrent.ExecutionExceptionjava.util.concurrent.TimeoutExceptionpublic <T> java.util.List<java.util.concurrent.Future<T>> invokeAll(java.util.Collection<? extends java.util.concurrent.Callable<T>> tasks)
throws java.lang.InterruptedException
invokeAll in interface java.util.concurrent.ExecutorServiceinvokeAll in class java.util.concurrent.AbstractExecutorServicejava.lang.InterruptedExceptionpublic <T> java.util.List<java.util.concurrent.Future<T>> invokeAll(java.util.Collection<? extends java.util.concurrent.Callable<T>> tasks,
long timeout,
java.util.concurrent.TimeUnit unit)
throws java.lang.InterruptedException
invokeAll in interface java.util.concurrent.ExecutorServiceinvokeAll in class java.util.concurrent.AbstractExecutorServicejava.lang.InterruptedExceptionprivate void throwIfInEventLoop(java.lang.String method)
public 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 the
it is fully started.protected boolean wakesUpForTask(java.lang.Runnable task)
protected static void reject()
protected final void reject(java.lang.Runnable task)
RejectedExecutionHandler.task - to reject.private void startThread()
private void doStartThread()