This section describes Netty components

EventLoop

In Java NIO to Netty (4), you can see that we register both ServerSocketChannel and SocketChannel with a Selector, which in a while loop polls to see if a Channel is ready for an event. As shown in the figure below

As can be seen from the above figure, both listening for new connections and reading and writing to sockets are completed in one thread. An EventLoop can also be understood simply as a thread that is bound internally to a Selector to handle connections, read and write events, etc. Let’s go back to code (1)

EventLoopGroup bossGroup = new NioEventLoopGroup(); 
EventLoopGroup workerGroup = newNioEventLoopGroup(); . b.group(bossGroup, workerGroup)Copy the code

Here we set up two EventLoop thread groups and set them to ServerBootstrap. BossGroup is for interface connection requests (OP_ACCEPT events) and workerGroup is for I/O operations (OP_READ events). It can be expressed as follows: