Demystifying Netty's Main, Boss, and Worker Threads in Java NIO
This article explains how Netty creates and uses the main thread, boss thread, and worker threads through NioEventLoopGroup and selectors, detailing the initialization of ServerSocketChannel, event registration, and the transition from OP_ACCEPT registration to active listening.
When we write our own thread inside the main method, it runs on the main thread.
Main Thread
Creating a NioEventLoopGroup (a group of NioEventLoops) also creates a Selector:
Because the current thread is the main thread, the selector’s isOpen flag is false:
Next, Netty creates a ServerSocketChannel:
Initialize the server socket channel.
Select a NioEventLoop from the boss group for the server socket channel.
Boss Thread
The selected NioEventLoop registers the ServerSocketChannel to its selector:
It then binds the address and starts listening:
The accept event ( OP_ACCEPT) is registered to the selector, but the initial interest ops value is 0 rather than OP_ACCEPT:
After the bind completes, OP_ACCEPT is set and fireChannelActive() is triggered, which finally starts listening:
The NioEventLoop becomes active through the execution of the Register operation.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
JavaEdge
First‑line development experience at multiple leading tech firms; now a software architect at a Shanghai state‑owned enterprise and founder of Programming Yanxuan. Nearly 300k followers online; expertise in distributed system design, AIGC application development, and quantitative finance investing.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
