public abstract class SingleThreadEventExecutor extends AbstractEventExecutor
EventExecutor
's that execute all its submitted tasks in a single thread.Modifier | Constructor and Description |
---|---|
protected |
SingleThreadEventExecutor(EventExecutorGroup parent,
ThreadFactory threadFactory,
boolean addTaskWakesUp)
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. |
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 |
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 . |
boolean |
isShutdown() |
boolean |
isShuttingDown()
|
boolean |
isTerminated() |
protected Queue<Runnable> |
newTaskQueue()
Create a new
Queue which will holds the tasks to execute. |
EventExecutorGroup |
parent()
Return the
EventExecutorGroup which is the parent of this EventExecutor , |
protected Runnable |
peekTask() |
int |
pendingTasks()
Return the number of tasks that are pending for processing.
|
protected Runnable |
pollTask() |
protected static void |
reject() |
void |
removeShutdownHook(Runnable task)
Remove a previous added
Runnable as a shutdown hook |
protected boolean |
removeTask(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. |
<V> ScheduledFuture<V> |
schedule(Callable<V> callable,
long delay,
TimeUnit unit) |
ScheduledFuture<?> |
schedule(Runnable command,
long delay,
TimeUnit unit) |
ScheduledFuture<?> |
scheduleAtFixedRate(Runnable command,
long initialDelay,
long period,
TimeUnit unit) |
ScheduledFuture<?> |
scheduleWithFixedDelay(Runnable command,
long initialDelay,
long delay,
TimeUnit unit) |
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 this executor has been terminated. |
protected void |
updateLastExecutionTime()
Updates the internal timestamp that tells when a submitted task was executed most recently.
|
protected void |
wakeup(boolean inEventLoop) |
inEventLoop, iterator, newFailedFuture, newProgressivePromise, newPromise, newSucceededFuture, newTaskFor, newTaskFor, next, shutdownGracefully, shutdownNow, submit, submit, submit
invokeAll, invokeAll, invokeAny, invokeAny
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
invokeAll, invokeAll, invokeAny, invokeAny
protected 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 Thread
addTaskWakesUp
- true
if and only if invocation of addTask(Runnable)
will wake up the
executor threadprotected Queue<Runnable> newTaskQueue()
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
,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()
Queue#isEmpty()}
public final int pendingTasks()
protected void addTask(Runnable task)
RejectedExecutionException
if this instance was shutdown
before.protected boolean removeTask(Runnable task)
Queue#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(Thread thread)
EventExecutor
public 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)
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.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.@Deprecated public void shutdown()
shutdown
in interface EventExecutorGroup
shutdown
in interface ExecutorService
shutdown
in class AbstractEventExecutor
public boolean isShuttingDown()
EventExecutorGroup
public boolean isShutdown()
public boolean isTerminated()
protected boolean confirmShutdown()
public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException
InterruptedException
public void execute(Runnable task)
protected static void reject()
public ScheduledFuture<?> schedule(Runnable command, long delay, TimeUnit unit)
schedule
in interface EventExecutorGroup
schedule
in interface ScheduledExecutorService
schedule
in class AbstractEventExecutor
public <V> ScheduledFuture<V> schedule(Callable<V> callable, long delay, TimeUnit unit)
schedule
in interface EventExecutorGroup
schedule
in interface ScheduledExecutorService
schedule
in class AbstractEventExecutor
public ScheduledFuture<?> scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit)
scheduleAtFixedRate
in interface EventExecutorGroup
scheduleAtFixedRate
in interface ScheduledExecutorService
scheduleAtFixedRate
in class AbstractEventExecutor
public ScheduledFuture<?> scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit)
scheduleWithFixedDelay
in interface EventExecutorGroup
scheduleWithFixedDelay
in interface ScheduledExecutorService
scheduleWithFixedDelay
in class AbstractEventExecutor
Copyright © 2008–2013 The Netty Project. All rights reserved.