public abstract class SingleThreadEventExecutor extends AbstractScheduledEventExecutor implements OrderedEventExecutor
OrderedEventExecutor
's that execute all its submitted tasks in a single thread.限定符 | 构造器和说明 |
---|---|
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
|
限定符和类型 | 方法和说明 |
---|---|
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. |
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.
|
void |
execute(java.lang.Runnable task) |
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 this executor was started to be
shut down gracefuclly or was shut down. |
boolean |
isTerminated() |
protected java.util.Queue<java.lang.Runnable> |
newTaskQueue()
已过时。
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. |
EventExecutorGroup |
parent()
Return the
EventExecutorGroup which is the parent of this EventExecutor , |
protected java.lang.Runnable |
peekTask() |
int |
pendingTasks()
Return the number of tasks that are pending for processing.
|
protected java.lang.Runnable |
pollTask() |
protected static void |
reject() |
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. |
void |
shutdown()
已过时。
|
Future<?> |
shutdownGracefully(long quietPeriod,
long timeout,
java.util.concurrent.TimeUnit unit)
Signals this executor that the caller wants the executor to be shut down.
|
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 this executor has been terminated. |
ThreadProperties |
threadProperties()
|
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, pollScheduledTask, pollScheduledTask, schedule, schedule, scheduleAtFixedRate, scheduleWithFixedDelay
inEventLoop, iterator, newFailedFuture, newProgressivePromise, newPromise, newSucceededFuture, newTaskFor, newTaskFor, next, shutdownGracefully, shutdownNow, submit, submit, submit
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
inEventLoop, newFailedFuture, newProgressivePromise, newPromise, newSucceededFuture, next
iterator, schedule, schedule, scheduleAtFixedRate, scheduleWithFixedDelay, shutdownGracefully, shutdownNow, submit, submit, submit
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 Thread
addTaskWakesUp
- 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 Thread
addTaskWakesUp
- 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.public EventExecutorGroup parent()
EventExecutor
EventExecutorGroup
which is the parent of this EventExecutor
,parent
在接口中 EventExecutor
protected void interruptThread()
Thread
.protected java.lang.Runnable pollTask()
Queue.poll()
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.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.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 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 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)
EventExecutor
true
if the given Thread
is executed in the event loop,
false
otherwise.inEventLoop
在接口中 EventExecutor
public 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 hookpublic Future<?> shutdownGracefully(long quietPeriod, long timeout, java.util.concurrent.TimeUnit unit)
EventExecutorGroup
EventExecutorGroup.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
在接口中 EventExecutorGroup
quietPeriod
- 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 timeout
EventExecutorGroup.terminationFuture()
public Future<?> terminationFuture()
EventExecutorGroup
Future
which is notified when this executor has been terminated.terminationFuture
在接口中 EventExecutorGroup
@Deprecated public void shutdown()
shutdown
在接口中 EventExecutorGroup
shutdown
在接口中 java.util.concurrent.ExecutorService
shutdown
在类中 AbstractEventExecutor
public boolean isShuttingDown()
EventExecutorGroup
true
if and only if this executor was started to be
shut down gracefuclly or was shut down.isShuttingDown
在接口中 EventExecutorGroup
public boolean isShutdown()
isShutdown
在接口中 java.util.concurrent.ExecutorService
public boolean isTerminated()
isTerminated
在接口中 java.util.concurrent.ExecutorService
protected boolean confirmShutdown()
public boolean awaitTermination(long timeout, java.util.concurrent.TimeUnit unit) throws java.lang.InterruptedException
awaitTermination
在接口中 java.util.concurrent.ExecutorService
java.lang.InterruptedException
public void execute(java.lang.Runnable task)
execute
在接口中 java.util.concurrent.Executor
public <T> T invokeAny(java.util.Collection<? extends java.util.concurrent.Callable<T>> tasks) throws java.lang.InterruptedException, java.util.concurrent.ExecutionException
invokeAny
在接口中 java.util.concurrent.ExecutorService
invokeAny
在类中 java.util.concurrent.AbstractExecutorService
java.lang.InterruptedException
java.util.concurrent.ExecutionException
public <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
在接口中 java.util.concurrent.ExecutorService
invokeAny
在类中 java.util.concurrent.AbstractExecutorService
java.lang.InterruptedException
java.util.concurrent.ExecutionException
java.util.concurrent.TimeoutException
public <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
在接口中 java.util.concurrent.ExecutorService
invokeAll
在类中 java.util.concurrent.AbstractExecutorService
java.lang.InterruptedException
public <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
在接口中 java.util.concurrent.ExecutorService
invokeAll
在类中 java.util.concurrent.AbstractExecutorService
java.lang.InterruptedException
public final ThreadProperties threadProperties()
protected boolean wakesUpForTask(java.lang.Runnable task)
protected static void reject()