Introduction
In netty, whether it is ServerBootstrap on the server side or Bootstrap on the client side, an EventLoopGroup parameter needs to be passed in the group method when it is created to process all IO operations and events in all ServerChannels and Channels.
Some friends may have looked at the source code of netty a little, and may find that there is a class very similar to EventLoopGroup called EventLoop. So what is the relationship between EventLoopGroup and EventLoop? What is the interaction between their bottom layer and channel? Let's take a look together.
EventLoopGroup and EventLoop
EventLoopGroup inherits from EventExecutorGroup:
public interface EventLoopGroup extends EventExecutorGroup
As we mentioned in the previous article, there is a next method in EventExecutorGroup that can return the corresponding EventExecutor. This method is rewritten in EventLoopGroup:
EventLoop next();
The next method returns no longer an EventExecutor, but an EventLoop.
In fact, the relationship between EventLoop and EventLoopGroup is somewhat similar to the relationship between EventExecutor and EventExecutorGroup. EventLoop is also inherited from EventLoopGroup, which is a collection of EventLoop.
public interface EventLoop extends OrderedEventExecutor, EventLoopGroup
In EventLoopGroup, in addition to the rewritten next method, the channel registration method register is also added, which is used to register the channel and EventLoop in the EventLoop, thereby realizing the binding of the channel and the EventLoop.
ChannelFuture register(Channel channel);
In EventLoop, a parent method has been added to represent the association between EventLoop and EventLoopGroup:
EventLoopGroup parent();
Default implementation of EventLoopGroup in netty
The default implementation of EventLoopGroup in netty is called DefaultEventLoopGroup. Let's take a look at its inheritance relationship:
<img src="https://img-blog.csdnimg.cn/119283e9b8d04854940abc0fc159c604.png" style="zoom:67%;" />
If you read the friends I explained about EventExecutorGroup before, you can see that the inheritance relationship between DefaultEventLoopGroup and DefaultEventExecutorGroup is very similar, DefaultEventLoopGroup inherits from MultithreadEventLoopGroup, and MultithreadEventLoopGroup inherits from MultithreadEventExecutorGroup and implements the EventLoopGroup interface:
public abstract class MultithreadEventLoopGroup extends MultithreadEventExecutorGroup implements EventLoopGroup
MultithreadEventLoopGroup uses multiple threads to process Event Loop.
A DEFAULT_EVENT_LOOP_THREADS is defined in MultithreadEventLoopGroup to store the default number of threads processing Event Loop:
DEFAULT_EVENT_LOOP_THREADS = Math.max(1, SystemPropertyUtil.getInt(
"io.netty.eventLoopThreads", NettyRuntime.availableProcessors() * 2));
For several newly added register methods in EventLoopGroup, MultithreadEventLoopGroup is implemented by calling the corresponding next method:
public ChannelFuture register(Channel channel) {
return next().register(channel);
}
The implementation of the next() method here actually calls the next method of the parent class, which is the next method in the MultithreadEventExecutorGroup, to select an EventLoop managed by the Group:
public EventLoop next() {
return (EventLoop) super.next();
}
For DefaultEventLoopGroup, it inherits from MultithreadEventLoopGroup and implements a newChild method to encapsulate the incoming executor into EventLoop:
protected EventLoop newChild(Executor executor, Object... args) throws Exception {
return new DefaultEventLoop(this, executor);
}
Default implementation of EventLoop in netty
The default implementation of EventLoop in netty is called DefaultEventLoop. Let's first look at its inheritance relationship:
<img src="https://img-blog.csdnimg.cn/9c642b58f6c248f9bddfbb71799549f9.png" style="zoom:67%;" />
DefaultEventLoop inherits from SingleThreadEventLoop, and SingleThreadEventLoop inherits from SingleThreadEventExecutor and implements the EventLoop interface.
Let's first look at the implementation of SingleThreadEventLoop:
public abstract class SingleThreadEventLoop extends SingleThreadEventExecutor implements EventLoop
SingleThreadEventLoop uses a single thread to execute submitted tasks. How does it change from SingleThreadEventExecutor?
First, a tailTasks is provided to store pending tasks:
private final Queue<Runnable> tailTasks;
This tailTasks will be used to judge and operate the number of tasks:
final boolean removeAfterEventLoopIterationTask(Runnable task) {
return tailTasks.remove(ObjectUtil.checkNotNull(task, "task"));
}
protected boolean hasTasks() {
return super.hasTasks() || !tailTasks.isEmpty();
}
public int pendingTasks() {
return super.pendingTasks() + tailTasks.size();
}
The implementation of the register method in SingleThreadEventLoop finally calls the unsafe register method in the registered channel:
channel.unsafe().register(this, promise);
Let's take a look at DefaultEventLoop, DefaultEventLoop inherits from SingleThreadEventLoop:
public class DefaultEventLoop extends SingleThreadEventLoop
In addition to the constructor, DefaultEventLoop implements a run method for the execution logic of specific tasks:
protected void run() {
for (;;) {
Runnable task = takeTask();
if (task != null) {
task.run();
updateLastExecutionTime();
}
if (confirmShutdown()) {
break;
}
}
}
If the comparison can be found, the implementation of the run method in DefaultEventLoop and DefaultEventExecutor is the same.
Summarize
This article introduces the default implementations of EventLoop and EventLoopGroup in netty: DefaultEventLoop and DefaultEventLoopGroup, but I don't know if your friends have found them. Even in the simplest netty applications, these two default EventLoops are rarely seen. The most commonly used ones are NioEventLoopGroup and NioEventLoop. This is because DefaultEventLoop and DefaultEventLoopGroup only use multi-threading technology. One thread represents one EventLoop. If there are too many EventLoops, it may cause waste of threads and performance, so it is used in NioEventLoopGroup and NioEventLoop. NIO technology is adopted, and the efficiency of EventLoop is improved by using NIO technology such as channel and selector. For the detailed introduction of NioEventLoopGroup and NioEventLoop, we will explain in detail in the next chapter, so stay tuned.
This article has been included in http://www.flydean.com/05-1-netty-eventloop-eventloopgroup/
The most popular interpretation, the most profound dry goods, the most concise tutorials, and many tricks you don't know are waiting for you to discover!
Welcome to pay attention to my official account: "Program those things", understand technology, understand you better!
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。