Netty 中的 I/O 个线程是什么?

What are I/O threads in Netty?

我阅读了 the article about thread model in Netty 并且对 Netty 中的 IO 有疑问。考虑以下 ServerBootstrap 声明:

NioEventLoopGroup workerGroup = new NioEventLoopGroup(16)
new ServerBootstrap()
.childHandler(
     new ChannelInitializer<Channel> {
          override def initChannel(ch: Channel) = ch.pipeline()
            .addLast(new ChannelDuplexHandler)   // Without specifying event-loop-group
            .addLast(workerGroup, new HttpRequestDecoder()) //event group specified
    }

据我了解,ChannelDuplexHandler 将直接从 IO 线程调用。

问题是如何配置 IO 线程(更改 IO 线程的数量,也许覆盖 IO 线程以定义我的自定义中断行为)?

我可以让我的事件循环组成为一个 IO 组吗?我是说

NioEventLoopGroup myIoGroup = new NioEventLoopGroup(16); 
                              // Is it possible to make it IO-group?

我对你的问题有点困惑,所以我希望这能回答...。EventLoopGroup 使用的线程是 "IO-threads",你传入的数字是数字您要使用的 "IO-threads" 个。这些线程中的每一个都将处理 0-n 个通道。要增加 IO 线程的数量,您可以在此处指定一个与“16”不同的数字。

每个频道只会使用一个 "IO-thread"。如果你想确保 ChannelHandler 将从 IO 线程中卸载,你通常会创建一个 DefaultEventExecutorGroup 并在添加 ChannelHandler 时指定它。 EventExecutorGroup 应该在不同的 Channel 实例之间共享,以充分利用线程。

像这样:

NioEventLoopGroup workerGroup = new NioEventLoopGroup(16)        
EventExecutorGroup executorGroup = new DefaultEventExecutorGroup(numberOfThreads);
new ServerBootstrap()
    .childHandler(
         new ChannelInitializer<Channel> {
             override def initChannel(ch: Channel) = ch.pipeline()
                 .addLast(new ChannelDuplexHandler)   // Without specifying event-loop-group
                 .addLast(executorGroup, new HttpRequestDecoder()) 
}