Class SingleThreadEventExecutor

    • Field Detail

      • DEFAULT_MAX_PENDING_EXECUTOR_TASKS

        protected static final int DEFAULT_MAX_PENDING_EXECUTOR_TASKS
    • Constructor Detail

      • SingleThreadEventExecutor

        public SingleThreadEventExecutor()
        Create a new instance
      • SingleThreadEventExecutor

        public SingleThreadEventExecutor​(ThreadFactory threadFactory)
        Create a new instance
        Parameters:
        threadFactory - the ThreadFactory which will be used for the used Thread
      • SingleThreadEventExecutor

        public SingleThreadEventExecutor​(ThreadFactory threadFactory,
                                         int maxPendingTasks,
                                         RejectedExecutionHandler rejectedHandler)
        Create a new instance
        Parameters:
        threadFactory - the ThreadFactory which will be used for the used Thread
        maxPendingTasks - the maximum number of pending tasks before new tasks will be rejected.
        rejectedHandler - the RejectedExecutionHandler to use.
      • SingleThreadEventExecutor

        public SingleThreadEventExecutor​(Executor executor)
        Create a new instance
        Parameters:
        executor - the Executor which will be used for executing
      • SingleThreadEventExecutor

        public SingleThreadEventExecutor​(Executor executor,
                                         int maxPendingTasks,
                                         RejectedExecutionHandler rejectedHandler)
        Create a new instance
        Parameters:
        executor - the Executor which will be used for executing
        maxPendingTasks - the maximum number of pending tasks before new tasks will be rejected.
        rejectedHandler - the RejectedExecutionHandler to use.
    • Method Detail

      • newTaskQueue

        protected Queue<Runnable> newTaskQueue​(int maxPendingTasks)
        Create a new 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. Be aware that the implementation of run() depends on a BlockingQueue so you will need to override run() as well if you return a non BlockingQueue from this method. As this method is called from within the constructor you can only use the parameters passed into the method when overriding this method.
      • interruptThread

        protected final void interruptThread()
        Interrupt the current running Thread.
      • pendingTasks

        public final int pendingTasks()
        Return the number of tasks that are pending for processing (excluding the scheduled tasks).
      • runAllTasks

        protected int runAllTasks​(int maxTasks)
        Poll all tasks from the task queue and run them via Runnable.run() method. This method must be called from the EventExecutor thread.
        Returns:
        the number of processed tasks.
      • delayNanos

        protected final long delayNanos​(long currentTimeNanos)
        Returns the amount of time left until the scheduled task with the closest dead line is executed. This method must be called from the EventExecutor thread.
      • updateLastExecutionTime

        protected final void updateLastExecutionTime()
        Updates the internal timestamp that tells when a submitted task was executed most recently. runAllTasks(int) 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 if you execute a task for accurate quiet period checks. This method must be called from the EventExecutor thread.
      • cleanup

        protected void cleanup()
        Do nothing, sub-classes may override.
      • wakeup

        protected void wakeup​(boolean inEventLoop)
      • inEventLoop

        public final boolean inEventLoop​(Thread thread)
        Description copied from interface: EventExecutor
        Return true if the given Thread is executed in the event loop, false otherwise.
        Specified by:
        inEventLoop in interface EventExecutor
      • addShutdownHook

        public final void addShutdownHook​(Runnable task)
        Add a Runnable which will be executed on shutdown of this instance
      • removeShutdownHook

        public final void removeShutdownHook​(Runnable task)
        Remove a previous added Runnable as a shutdown hook
      • shutdownGracefully

        public final Future<Void> shutdownGracefully​(long quietPeriod,
                                                     long timeout,
                                                     TimeUnit unit)
        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 return true, and the executor prepares to shut itself down. This method 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 interface EventExecutorGroup
        Parameters:
        quietPeriod - the quiet period as described in the documentation
        timeout - the maximum amount of time to wait until the executor is shutting down regardless if a task was submitted during the quiet period.
        unit - the unit of quietPeriod and timeout
        Returns:
        the EventExecutorGroup.terminationFuture()
      • isShuttingDown

        public final boolean isShuttingDown()
        Description copied from interface: EventExecutorGroup
        Returns true if and only if all EventExecutors managed by this EventExecutorGroup are being shut down gracefully or was shut down.

        An executor group that "is shutting down" can still accept new tasks for a little while (the grace period), but will eventually start rejecting new tasks. At that point, the executor group will be shut down.

        Specified by:
        isShuttingDown in interface EventExecutorGroup
        Returns:
        true if all executors in this group have at least started shutting down, otherwise false.
      • isShutdown

        public final boolean isShutdown()
        Description copied from interface: EventExecutorGroup
        Returns true if all EventExecutors managed by this EventExecutorGroup have been shut down gracefully and moved past the grace period so that they are no longer accepting any new tasks.

        An executor group that "is shut down" might still be executing tasks that it has queued up, but it will no longer be accepting any new tasks. Once all running and queued tasks have completed, the executor group will be terminated.

        Specified by:
        isShutdown in interface EventExecutorGroup
        Returns:
        true if all executors in this group have shut down and are no longer accepting any new tasks.
      • confirmShutdown

        protected final boolean confirmShutdown()
        Confirm that the shutdown if the instance should be done now! This method must be called from the EventExecutor thread.
      • wakesUpForTask

        protected boolean wakesUpForTask​(Runnable task)
        Returns true if wakeup(boolean) should be called for this Runnable, false otherwise.
      • reject

        protected static void reject()