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, ThreadAwareExecutor, java.lang.Iterable<EventExecutor>, java.util.concurrent.Executor, java.util.concurrent.ExecutorService, java.util.concurrent.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 ClassesModifier and TypeClassDescriptionprivate static final class(package private) final classNested classes/interfaces inherited from class AbstractEventExecutor
AbstractEventExecutor.LazyRunnable -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final GlobalEventExecutorprivate static final InternalLogger(package private) final ScheduledFutureTask<java.lang.Void> private static final longprivate final java.util.concurrent.atomic.AtomicBoolean(package private) final java.util.concurrent.BlockingQueue<java.lang.Runnable> private final GlobalEventExecutor.TaskRunnerprivate final Future<?> (package private) java.lang.Thread(package private) final java.util.concurrent.ThreadFactoryFields inherited from class AbstractScheduledEventExecutor
nextTaskId, scheduledTaskQueue, WAKEUP_TASKFields inherited from class AbstractEventExecutor
DEFAULT_SHUTDOWN_QUIET_PERIOD, DEFAULT_SHUTDOWN_TIMEOUT -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate voidaddTask(java.lang.Runnable task) Add a task to the task queue, or throws aRejectedExecutionExceptionif this instance was shutdown before.booleanawaitInactivity(long timeout, java.util.concurrent.TimeUnit unit) Waits until the worker thread of this executor has no tasks left in its task queue and terminates itself.booleanawaitTermination(long timeout, java.util.concurrent.TimeUnit unit) voidexecute(java.lang.Runnable task) private voidexecute0(java.lang.Runnable task) private voidbooleaninEventLoop(java.lang.Thread thread) Returntrueif the givenThreadis executed in the event loop,falseotherwise.booleanbooleanReturnstrueif and only if allEventExecutors managed by thisEventExecutorGroupare being shut down gracefully or was shut down.booleanintReturn the number of tasks that are pending for processing.private static voidsetContextClassLoader(java.lang.Thread t, java.lang.ClassLoader cl) voidshutdown()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(package private) java.lang.RunnabletakeTask()Take the nextRunnablefrom the task queue and so will block if no task is currently present.Future<?> Returns theFuturewhich is notified when allEventExecutors managed by thisEventExecutorGrouphave been terminated.Methods inherited from class AbstractScheduledEventExecutor
afterScheduledTaskSubmitted, beforeScheduledTaskSubmitted, cancelScheduledTasks, deadlineNanos, deadlineToDelayNanos, defaultCurrentTimeNanos, delayNanos, fetchFromScheduledTaskQueue, getCurrentTimeNanos, hasScheduledTasks, initialNanoTime, nanoTime, nextScheduledTaskDeadlineNanos, nextScheduledTaskNano, peekScheduledTask, pollScheduledTask, pollScheduledTask, removeScheduled, schedule, schedule, scheduleAtFixedRate, scheduledTaskQueue, scheduleFromEventLoop, scheduleRemoveScheduled, scheduleWithFixedDelay, ticker, validateScheduledMethods inherited from class AbstractEventExecutor
iterator, lazyExecute, newTaskFor, newTaskFor, next, parent, runTask, safeExecute, shutdownGracefully, shutdownNow, submit, submit, submitMethods inherited from class java.util.concurrent.AbstractExecutorService
invokeAll, invokeAll, invokeAny, invokeAnyMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface EventExecutor
inEventLoop, isExecutorThread, isSuspended, newFailedFuture, newProgressivePromise, newPromise, newSucceededFuture, parent, trySuspendMethods inherited from interface EventExecutorGroup
iterator, next, schedule, schedule, scheduleAtFixedRate, scheduleWithFixedDelay, shutdownGracefully, shutdownNow, submit, submit, submit, tickerMethods inherited from interface java.util.concurrent.ExecutorService
invokeAll, invokeAll, invokeAny, invokeAnyMethods 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
final java.util.concurrent.BlockingQueue<java.lang.Runnable> taskQueue -
quietPeriodTask
-
threadFactory
final java.util.concurrent.ThreadFactory threadFactory -
taskRunner
-
started
private final java.util.concurrent.atomic.AtomicBoolean started -
thread
volatile java.lang.Thread thread -
terminationFuture
-
-
Constructor Details
-
GlobalEventExecutor
private GlobalEventExecutor()
-
-
Method Details
-
takeTask
java.lang.Runnable takeTask()Take the nextRunnablefrom the task queue and so will block if no task is currently present.- Returns:
nullif 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
private void addTask(java.lang.Runnable task) Add a task to the task queue, or throws aRejectedExecutionExceptionif this instance was shutdown before. -
inEventLoop
public boolean inEventLoop(java.lang.Thread thread) Description copied from interface:EventExecutorReturntrueif the givenThreadis executed in the event loop,falseotherwise.- Specified by:
inEventLoopin interfaceEventExecutor
-
shutdownGracefully
public Future<?> shutdownGracefully(long quietPeriod, long timeout, java.util.concurrent.TimeUnit unit) Description copied from interface:EventExecutorGroupSignals 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:
shutdownGracefullyin 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 ofquietPeriodandtimeout- Returns:
- the
EventExecutorGroup.terminationFuture()
-
terminationFuture
Description copied from interface:EventExecutorGroupReturns theFuturewhich is notified when allEventExecutors managed by thisEventExecutorGrouphave been terminated.- Specified by:
terminationFuturein interfaceEventExecutorGroup
-
shutdown
@Deprecated public void shutdown()Deprecated.- Specified by:
shutdownin interfaceEventExecutorGroup- Specified by:
shutdownin interfacejava.util.concurrent.ExecutorService- Specified by:
shutdownin classAbstractEventExecutor
-
isShuttingDown
public boolean isShuttingDown()Description copied from interface:EventExecutorGroupReturnstrueif and only if allEventExecutors managed by thisEventExecutorGroupare being shut down gracefully or was shut down.- Specified by:
isShuttingDownin interfaceEventExecutorGroup
-
isShutdown
public boolean isShutdown()- Specified by:
isShutdownin interfacejava.util.concurrent.ExecutorService
-
isTerminated
public boolean isTerminated()- Specified by:
isTerminatedin interfacejava.util.concurrent.ExecutorService
-
awaitTermination
public boolean awaitTermination(long timeout, java.util.concurrent.TimeUnit unit) - Specified by:
awaitTerminationin interfacejava.util.concurrent.ExecutorService
-
awaitInactivity
public boolean awaitInactivity(long timeout, java.util.concurrent.TimeUnit unit) throws java.lang.InterruptedException 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:
trueif and only if the worker thread has been terminated- Throws:
java.lang.InterruptedException
-
execute
public void execute(java.lang.Runnable task) - Specified by:
executein interfacejava.util.concurrent.Executor
-
execute0
private void execute0(java.lang.Runnable task) -
startThread
private void startThread() -
setContextClassLoader
private static void setContextClassLoader(java.lang.Thread t, java.lang.ClassLoader cl)
-