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

标签 java multithreading netty

我读到the article关于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 线程”,你传入的数字是您想要使用的“IO 线程”数量。每个线程将处理 0-n 个 channel 。要增加 IO 线程的数量,您可以在此处指定一个与“16”不同的数字。

每个 channel 仅使用一个“IO 线程”。如果您想确保从 IO 线程中卸载 ChannelHandler,您通常会创建一个 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()) 
}

关于java - Netty 中的 I/O 线程是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47793460/

相关文章:

java - 无法从类 io.netty.channel.sctp.nio.NioSctpChannel 创建 channel

java - 为什么小于 int 的数据类型中的两个数字相加会转换为 int?

c# - 如何从 ASP.NET 发送大量电子邮件?

.net - 对int []数组的并发读取访问: Is it safe?快速吗?

redis - java.net.UnknownHostException : redisson-netty-2-6 ERROR DNSMonitor

java - netty 堆栈关闭后重用 NioEventLoopGroup

java - 我的 RecyclerView 在过滤后不断重置

Java AES 加密(工作)和 Python 解密(不工作)

java - 设置 Cloud9 IDE 来编译和运行 Java?

c - 如何测量openmp中每个线程的执行时间?