In most of the examples that i have currently using Netty, I see that boss executor is a cached threadpool. From what i understand the event loop should only have a single thread, why exactly do we need a cached threadpool for the boss event loop?
Netty has the concept of EventLoopGroup which aggregates several EventLoops. An EventLoop is single threaded, the EventLoopGroup is mostly not. Unless you're doing old blocking I/O.
Now, what concerns boss vs. workers. Assuming you're talking about a server implementation, the boss is now called parentGroup and the workers childGroup. The parent handles the I/O for the acceptor channel, i.e. the one which is bounded to the port where your server is accepting new requests. The children will the handling the I/O for the accepted connections. Indeed the acceptor channel should be handled by one and only thread and it will. If you pass a EventLoopGroup with several EventLoops to the parent, it will only use one of the EventLoops anyway, so a good practice is to reuse the same EventLoopGroup for both parent and child.
The ServerBootstrap actually provides two constructors, one that takes a single group and reuses it for both parent and client:
@Override
public ServerBootstrap group(EventLoopGroup group) {
return group(group, group);
}
and one that allows you to explicitly pass the one for the parent and the one for the child:
public ServerBootstrap group(EventLoopGroup parentGroup, EventLoopGroup childGroup) {
super.group(parentGroup);
if (childGroup == null) {
throw new NullPointerException("childGroup");
}
if (this.childGroup != null) {
throw new IllegalStateException("childGroup set already");
}
this.childGroup = childGroup;
return this;
}
You can check it here: https://github.com/netty/netty/blob/master/transport/src/main/java/io/netty/bootstrap/ServerBootstrap.java
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With