本文介绍了Netty如何使用线程池?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Can you please explain how Netty uses thread pools to work? Do I understand correctly, that there are two kinds of thread-pools: boss and worker. Boss ones are used to do I/O and worker are used to call user callbacks (messageReceived) to process data?

推荐答案

这个来自NioServerSocketChannelFactory文档

This is from NioServerSocketChannelFactory document

线程如何工作


NioServerSocketChannelFactory中有两种类型的
线程;一个是
boss线程,另一个是worker
线程。

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

Boss线程

每个绑定
ServerSocketChannel都有自己的boss
线程。例如,如果您打开了两个
服务器端口,例如80和443,那么
将有两个boss线程。 boss
线程接受传入连接
,直到端口未绑定。一旦
连接成功被接受,
boss线程将接受的
Channel传递给NioServerSocketChannelFactory
管理的工作线程
之一。

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.

工作线程

一个
NioServerSocketChannelFactory可以有一个或多个工作线程
。一个worker
线程以
非阻塞模式为一个或多个Channel执行非阻塞读取和
写入。

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)。

In Nio model, bossThread take care all bounded socket(listen socket), workerThread take care Accepted-socket (included IO and call event method such as messageReceived).

这篇关于Netty如何使用线程池?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-14 19:44