gpt4 book ai didi

java - Netty 如何使用线程池?

转载 作者:IT老高 更新时间:2023-10-28 20:55:49 33 4
gpt4 key购买 nike

你能解释一下 Netty 是如何使用线程池工作的吗?我是否理解正确,有两种线程池:老板和 worker 。 boss用来做I/O,worker用来调用用户回调(messageReceived)处理数据?

最佳答案

这是来自 NioServerSocketChannelFactory 文档

A ServerSocketChannelFactory which creates a server-side NIO-based ServerSocketChannel. It utilizes the non-blocking I/O mode which was introduced with NIO to serve many number of concurrent connections efficiently.

How threads work
There are two types of threads in a NioServerSocketChannelFactory; one is boss thread and the other is worker thread.

Boss threads
Each bound ServerSocketChannel has its own boss thread. For example, if you opened two server ports such as 80 and 443, you will have two boss threads. A boss thread accepts incoming connections until the port is unbound. Once a connection is accepted successfully, the boss thread passes the accepted Channel to one of the worker threads that the NioServerSocketChannelFactory manages.

Worker threads
One NioServerSocketChannelFactory can have one or more worker threads. A worker thread performs non-blocking read and write for one or more Channels in a non-blocking mode.

在 Nio 模型中,bossThread 负责所有有界套接字(listen socket),workerThread 负责 Accepted-socket(包括 IO 和调用 messageReceived 等事件方法)。

关于java - Netty 如何使用线程池?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5474372/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com