Home / Class/ TaskRunner Class — netty Architecture

TaskRunner Class — netty Architecture

Architecture documentation for the TaskRunner class in GlobalEventExecutor.java from the netty codebase.

Entity Profile

Dependency Diagram

graph TD
  391a7615_0efb_7213_6501_1dcb645200b7["TaskRunner"]
  b64f70b1_1ecd_fc71_d0ac_3c49aebb5e9e["GlobalEventExecutor.java"]
  391a7615_0efb_7213_6501_1dcb645200b7 -->|defined in| b64f70b1_1ecd_fc71_d0ac_3c49aebb5e9e
  9f953b79_2e4a_57b7_6141_d89c69f7af26["run()"]
  391a7615_0efb_7213_6501_1dcb645200b7 -->|method| 9f953b79_2e4a_57b7_6141_d89c69f7af26

Relationship Graph

Source Code

common/src/main/java/io/netty/util/concurrent/GlobalEventExecutor.java lines 279–329

    final class TaskRunner implements Runnable {
        @Override
        public void run() {
            for (;;) {
                Runnable task = takeTask();
                if (task != null) {
                    try {
                        runTask(task);
                    } catch (Throwable t) {
                        logger.warn("Unexpected exception from the global event executor: ", t);
                    }

                    if (task != quietPeriodTask) {
                        continue;
                    }
                }

                Queue<ScheduledFutureTask<?>> scheduledTaskQueue = GlobalEventExecutor.this.scheduledTaskQueue;
                // Terminate if there is no task in the queue (except the noop task).
                if (taskQueue.isEmpty() && (scheduledTaskQueue == null || scheduledTaskQueue.size() == 1)) {
                    // Mark the current thread as stopped.
                    // The following CAS must always success and must be uncontended,
                    // because only one thread should be running at the same time.
                    boolean stopped = started.compareAndSet(true, false);
                    assert stopped;

                    // Check if there are pending entries added by execute() or schedule*() while we do CAS above.
                    // Do not check scheduledTaskQueue because it is not thread-safe and can only be mutated from a
                    // TaskRunner actively running tasks.
                    if (taskQueue.isEmpty()) {
                        // A) No new task was added and thus there's nothing to handle
                        //    -> safe to terminate because there's nothing left to do
                        // B) A new thread started and handled all the new tasks.
                        //    -> safe to terminate the new thread will take care the rest
                        break;
                    }

                    // There are pending tasks added again.
                    if (!started.compareAndSet(false, true)) {
                        // startThread() started a new thread and set 'started' to true.
                        // -> terminate this thread so that the new thread reads from taskQueue exclusively.
                        break;
                    }

                    // New tasks were added, but this worker was faster to set 'started' to true.
                    // i.e. a new worker thread was not started by startThread().
                    // -> keep this thread alive to handle the newly added entries.
                }
            }
        }
    }

Frequently Asked Questions

What is the TaskRunner class?
TaskRunner is a class in the netty codebase, defined in common/src/main/java/io/netty/util/concurrent/GlobalEventExecutor.java.
Where is TaskRunner defined?
TaskRunner is defined in common/src/main/java/io/netty/util/concurrent/GlobalEventExecutor.java at line 279.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free