Home / Function/ run() — netty Function Reference

run() — netty Function Reference

Architecture documentation for the run() function in NonStickyEventExecutorGroup.java from the netty codebase.

Function java CommonUtil Concurrent calls 1 called by 1

Entity Profile

Dependency Diagram

graph TD
  26597b68_1b9b_0daa_4db4_2de2cf123abc["run()"]
  0a6a7b0f_e4f7_262e_c01d_c793011590ee["NonStickyOrderedEventExecutor"]
  26597b68_1b9b_0daa_4db4_2de2cf123abc -->|defined in| 0a6a7b0f_e4f7_262e_c01d_c793011590ee
  f324b658_e5d7_1d39_280a_67ba14086087["execute()"]
  f324b658_e5d7_1d39_280a_67ba14086087 -->|calls| 26597b68_1b9b_0daa_4db4_2de2cf123abc
  4bceba0d_15d4_a9d8_93ab_0be435138980["execute()"]
  26597b68_1b9b_0daa_4db4_2de2cf123abc -->|calls| 4bceba0d_15d4_a9d8_93ab_0be435138980
  style 26597b68_1b9b_0daa_4db4_2de2cf123abc fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

common/src/main/java/io/netty/util/concurrent/NonStickyEventExecutorGroup.java lines 235–294

        @Override
        public void run() {
            if (!state.compareAndSet(SUBMITTED, RUNNING)) {
                return;
            }
            Thread current = Thread.currentThread();
            executingThread.set(current);
            for (;;) {
                int i = 0;
                try {
                    for (; i < maxTaskExecutePerRun; i++) {
                        Runnable task = tasks.poll();
                        if (task == null) {
                            break;
                        }
                        safeExecute(task);
                    }
                } finally {
                    if (i == maxTaskExecutePerRun) {
                        try {
                            state.set(SUBMITTED);
                            // Only set executingThread to null if no other thread did update it yet.
                            executingThread.compareAndSet(current, null);
                            executor.execute(this);
                            return; // done
                        } catch (Throwable ignore) {
                            // Restore executingThread since we're continuing to execute tasks.
                            executingThread.set(current);
                            // Reset the state back to running as we will keep on executing tasks.
                            state.set(RUNNING);
                            // if an error happened we should just ignore it and let the loop run again as there is not
                            // much else we can do. Most likely this was triggered by a full task queue. In this case
                            // we just will run more tasks and try again later.
                        }
                    } else {
                        state.set(NONE);
                        // After setting the state to NONE, look at the tasks queue one more time.
                        // If it is empty, then we can return from this method.
                        // Otherwise, it means the producer thread has called execute(Runnable)
                        // and enqueued a task in between the tasks.poll() above and the state.set(NONE) here.
                        // There are two possible scenarios when this happens
                        //
                        // 1. The producer thread sees state == NONE, hence the compareAndSet(NONE, SUBMITTED)
                        //    is successfully setting the state to SUBMITTED. This mean the producer
                        //    will call / has called executor.execute(this). In this case, we can just return.
                        // 2. The producer thread don't see the state change, hence the compareAndSet(NONE, SUBMITTED)
                        //    returns false. In this case, the producer thread won't call executor.execute.
                        //    In this case, we need to change the state to RUNNING and keeps running.
                        //
                        // The above cases can be distinguished by performing a
                        // compareAndSet(NONE, RUNNING). If it returns "false", it is case 1; otherwise it is case 2.
                        if (tasks.isEmpty() || !state.compareAndSet(NONE, RUNNING)) {
                            // Only set executingThread to null if no other thread did update it yet.
                            executingThread.compareAndSet(current, null);
                            return; // done
                        }
                    }
                }
            }
        }

Domain

Subdomains

Calls

Called By

Frequently Asked Questions

What does run() do?
run() is a function in the netty codebase, defined in common/src/main/java/io/netty/util/concurrent/NonStickyEventExecutorGroup.java.
Where is run() defined?
run() is defined in common/src/main/java/io/netty/util/concurrent/NonStickyEventExecutorGroup.java at line 235.
What does run() call?
run() calls 1 function(s): execute.
What calls run()?
run() is called by 1 function(s): execute.

Analyze Your Own Codebase

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

Try Supermodel Free