Package io.netty.util.concurrent
Class GlobalEventExecutor
java.lang.Object
java.util.concurrent.AbstractExecutorService
io.netty.util.concurrent.AbstractEventExecutor
io.netty.util.concurrent.AbstractScheduledEventExecutor
io.netty.util.concurrent.GlobalEventExecutor
- All Implemented Interfaces:
EventExecutor
,EventExecutorGroup
,OrderedEventExecutor
,AutoCloseable
,Iterable<EventExecutor>
,Executor
,ExecutorService
,ScheduledExecutorService
public final class GlobalEventExecutor
extends AbstractScheduledEventExecutor
implements OrderedEventExecutor
Single-thread singleton
EventExecutor
. It starts the thread automatically and stops it when there is no
task pending in the task queue for io.netty.globalEventExecutor.quietPeriodSeconds
second
(default is 1 second). Please note it is not scalable to schedule large number of tasks to this executor;
use a dedicated executor.-
Nested Class Summary
Nested ClassesNested classes/interfaces inherited from class io.netty.util.concurrent.AbstractEventExecutor
AbstractEventExecutor.LazyRunnable
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final GlobalEventExecutor
private static final InternalLogger
(package private) final ScheduledFutureTask
<Void> private static final long
private final AtomicBoolean
(package private) final BlockingQueue
<Runnable> private final GlobalEventExecutor.TaskRunner
private final Future
<?> (package private) Thread
(package private) final ThreadFactory
Fields inherited from class io.netty.util.concurrent.AbstractScheduledEventExecutor
nextTaskId, scheduledTaskQueue, WAKEUP_TASK
Fields inherited from class io.netty.util.concurrent.AbstractEventExecutor
DEFAULT_SHUTDOWN_QUIET_PERIOD, DEFAULT_SHUTDOWN_TIMEOUT
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate void
Add a task to the task queue, or throws aRejectedExecutionException
if this instance was shutdown before.boolean
awaitInactivity
(long timeout, TimeUnit unit) Waits until the worker thread of this executor has no tasks left in its task queue and terminates itself.boolean
awaitTermination
(long timeout, TimeUnit unit) void
private void
private void
boolean
inEventLoop
(Thread thread) boolean
boolean
Returnstrue
if and only if allEventExecutor
s managed by thisEventExecutorGroup
are being shut down gracefully or was shut down.boolean
int
Return the number of tasks that are pending for processing.void
shutdown()
Deprecated.Future
<?> shutdownGracefully
(long quietPeriod, long timeout, TimeUnit unit) Signals this executor that the caller wants the executor to be shut down.private void
(package private) Runnable
takeTask()
Take the nextRunnable
from the task queue and so will block if no task is currently present.Future
<?> Returns theFuture
which is notified when allEventExecutor
s managed by thisEventExecutorGroup
have been terminated.Methods inherited from class io.netty.util.concurrent.AbstractScheduledEventExecutor
afterScheduledTaskSubmitted, beforeScheduledTaskSubmitted, cancelScheduledTasks, deadlineNanos, deadlineToDelayNanos, defaultCurrentTimeNanos, getCurrentTimeNanos, hasScheduledTasks, initialNanoTime, nanoTime, nextScheduledTaskDeadlineNanos, nextScheduledTaskNano, peekScheduledTask, pollScheduledTask, pollScheduledTask, removeScheduled, schedule, schedule, scheduleAtFixedRate, scheduledTaskQueue, scheduleFromEventLoop, scheduleWithFixedDelay, validateScheduled
Methods inherited from class io.netty.util.concurrent.AbstractEventExecutor
inEventLoop, iterator, lazyExecute, newFailedFuture, newProgressivePromise, newPromise, newSucceededFuture, newTaskFor, newTaskFor, next, parent, runTask, safeExecute, shutdownGracefully, shutdownNow, submit, submit, submit
Methods inherited from class java.util.concurrent.AbstractExecutorService
invokeAll, invokeAll, invokeAny, invokeAny
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface io.netty.util.concurrent.EventExecutor
inEventLoop, newFailedFuture, newProgressivePromise, newPromise, newSucceededFuture, next, parent
Methods inherited from interface io.netty.util.concurrent.EventExecutorGroup
iterator, schedule, schedule, scheduleAtFixedRate, scheduleWithFixedDelay, shutdownGracefully, shutdownNow, submit, submit, submit
Methods inherited from interface java.util.concurrent.ExecutorService
close, invokeAll, invokeAll, invokeAny, invokeAny
Methods inherited from interface java.lang.Iterable
forEach, spliterator
-
Field Details
-
logger
-
SCHEDULE_QUIET_PERIOD_INTERVAL
private static final long SCHEDULE_QUIET_PERIOD_INTERVAL -
INSTANCE
-
taskQueue
-
quietPeriodTask
-
threadFactory
-
taskRunner
-
started
-
thread
-
terminationFuture
-
-
Constructor Details
-
GlobalEventExecutor
private GlobalEventExecutor()
-
-
Method Details
-
takeTask
Runnable takeTask()Take the nextRunnable
from the task queue and so will block if no task is currently present.- Returns:
null
if the executor thread has been interrupted or waken up.
-
fetchFromScheduledTaskQueue
private void fetchFromScheduledTaskQueue() -
pendingTasks
public int pendingTasks()Return the number of tasks that are pending for processing. -
addTask
Add a task to the task queue, or throws aRejectedExecutionException
if this instance was shutdown before. -
inEventLoop
Description copied from interface:EventExecutor
- Specified by:
inEventLoop
in interfaceEventExecutor
-
shutdownGracefully
Description copied from interface:EventExecutorGroup
Signals this executor that the caller wants the executor to be shut down. Once this method is called,EventExecutorGroup.isShuttingDown()
starts to returntrue
, and the executor prepares to shut itself down. UnlikeEventExecutorGroup.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.- Specified by:
shutdownGracefully
in interfaceEventExecutorGroup
- Parameters:
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 ofquietPeriod
andtimeout
- Returns:
- the
EventExecutorGroup.terminationFuture()
-
terminationFuture
Description copied from interface:EventExecutorGroup
Returns theFuture
which is notified when allEventExecutor
s managed by thisEventExecutorGroup
have been terminated.- Specified by:
terminationFuture
in interfaceEventExecutorGroup
-
shutdown
Deprecated.- Specified by:
shutdown
in interfaceEventExecutorGroup
- Specified by:
shutdown
in interfaceExecutorService
- Specified by:
shutdown
in classAbstractEventExecutor
-
isShuttingDown
public boolean isShuttingDown()Description copied from interface:EventExecutorGroup
Returnstrue
if and only if allEventExecutor
s managed by thisEventExecutorGroup
are being shut down gracefully or was shut down.- Specified by:
isShuttingDown
in interfaceEventExecutorGroup
-
isShutdown
public boolean isShutdown()- Specified by:
isShutdown
in interfaceExecutorService
-
isTerminated
public boolean isTerminated()- Specified by:
isTerminated
in interfaceExecutorService
-
awaitTermination
- Specified by:
awaitTermination
in interfaceExecutorService
-
awaitInactivity
Waits until the worker thread of this executor has no tasks left in its task queue and terminates itself. Because a new worker thread will be started again when a new task is submitted, this operation is only useful when you want to ensure that the worker thread is terminated after your application is shut down and there's no chance of submitting a new task afterwards.- Returns:
true
if and only if the worker thread has been terminated- Throws:
InterruptedException
-
execute
-
execute0
-
startThread
private void startThread()
-