【发布时间】:2019-10-11 00:26:12
【问题描述】:
我正在设置一个 Netty 服务器来接受多个传入的客户端连接,这些连接会依次进行一些处理,但对 wroker 线程组与 Handler 线程感到困惑
我已尝试分配 10 个工作线程和 20 个处理程序线程,如下所示。
EventLoopGroup bossGroup = new NioEventLoopGroup(1);
EventLoopGroup workerGroup = new NioEventLoopGroup(10);
EventExecutorGroup handlerThread = new DefaultEventExecutorGroup(20);
try {
ServerBootstrap serverBootstrap = new ServerBootstrap();
serverBootstrap.group(bossGroup, workerGroup);
serverBootstrap.channel(NioServerSocketChannel.class);
serverBootstrap.localAddress(new
InetSocketAddress(hostName,Integer.parseInt(port)));
// initialize a new child handler for incoming request
logger.debug("Incoming request from TCP client...assigning a new Server Handler");
serverBootstrap.childHandler(new ChannelInitializer<SocketChannel>() {
protected void initChannel(SocketChannel socketChannel) throws Exception
{
socketChannel.pipeline().addLast(handlerThread,new NettyServerHandler());
}
});
ChannelFuture channelFuture = serverBootstrap.bind().sync();
channelFuture.channel().closeFuture().sync();
} catch (Exception e) {
logger.error("Unable to initialize TCP Server");
}
我无法理解工作组的任务和创建新的服务器处理程序之间的区别。据我了解,处理程序线程池将分配给 NettyServerHandler 的每个实例。但是那么创建 10 个线程的工作组池有什么作用呢?
【问题讨论】:
标签: netty