我读到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/