netty(3)—源码NioEventLoopGroup

一、概念

NioEventLoopGroup对象可以理解为一个线程池,内部维护了一组线程,每个线程负责处理多个Channel上的事件,而一个Channel只对应于一个线程,这样可以回避多线程下的数据同步问题。

我们先回顾下 上篇博客的服务器代码

        // 定义一对线程组
// 主线程组, 用于接受客户端的连接,但是不做任何处理,跟老板一样,不做事
EventLoopGroup bossGroup = new NioEventLoopGroup();
// 从线程组, 老板线程组会把任务丢给他,让手下线程组去做任务
EventLoopGroup workerGroup = new NioEventLoopGroup(); // netty服务器的创建, 辅助工具类,用于服务器通道的一系列配置
ServerBootstrap serverBootstrap = new ServerBootstrap();
serverBootstrap.group(bossGroup, workerGroup) //绑定两个线程组
//省略......

职责:

  1. 作为服务端 Acceptor 线程,负责处理客户端的请求接入。
  2. 作为客户端 Connector 线程,负责注册监听连接操作位,用于判断异步连接结果。
  3. 作为 IO 线程,监听网络读操作位,负责从 SocketChannel 中读取报文。
  4. 作为 IO 线程,负责向 SocketChannel 写入报文发送给对方,如果发生写半包,会自动注册监听写事件,用 于后续继续发送半包数据,直到数据全部发送完成。
  5. 作为定时任务线程,可以执行定时任务,例如链路空闲检测和发送心跳消息等。
  6. 作为线程执行器可以执行普通的任务线程(Runnable)。

二、NioEventLoopGroup源码分析

上面的代码 创建bossGroup及workerGroup时,使用了NioEventLoopGroup的无参构造方法,本篇将从此无参构造入手,详细分析NioEventLoopGroup的初始化过程。

    /**
* 1、首先我们看看NioEventLoopGroup的无参构造方法:
* 作用:线程数为0
*/
public NioEventLoopGroup() {
this(0);
} /**
* 2、继续调用构造函数。
* 作用:指定线程为0,且Executor为null
*/
public NioEventLoopGroup(int nThreads) {
this(nThreads, (Executor) null);
} /**
* 3、继续调用构造函数
* 作用:此构造方法它会指定selector的辅助类 "SelectorProvider.provider()"
*/
public NioEventLoopGroup(int nThreads, Executor executor) {
this(nThreads, executor, SelectorProvider.provider());
} /**
* 4、继续调用构造函数
* 作用:初始化了一个默认的选择策略工厂,用于生成select策略
*/
public NioEventLoopGroup(int nThreads, Executor executor, final SelectorProvider selectorProvider) {
this(nThreads, executor, selectorProvider, DefaultSelectStrategyFactory.INSTANCE);
} /**
* 5、继续调用构造函数
* 作用:指定拒绝策略:RejectedExecutionHandlers.reject()
*/
public NioEventLoopGroup(int nThreads, Executor executor, final SelectorProvider selectorProvider,final SelectStrategyFactory selectStrategyFactory) {
super(nThreads, executor, selectorProvider, selectStrategyFactory, RejectedExecutionHandlers.reject());
}

经过上面一系列的构造方法调用,此时参数值对应如下:

  1. nThreads: 0
  2. executor: null
  3. selectorProvider: SelectorProvider.provider()
  4. selectStrategyFactory: DefaultSelectStrategyFactory.INSTANCE
  5. 以及指定了拒绝策略: RejectedExecutionHandlers.reject()
    /**
* 6、从这里开始 调用父类 MultithreadEventLoopGroup 的构造函数
* 作用: 就是当指定的线程数为0时,使用默认的线程数DEFAULT_EVENT_LOOP_THREADS,
* 而DEFAULT_EVENT_LOOP_THREAD是在静态代码块中就被执行。
*/
protected MultithreadEventLoopGroup(int nThreads, Executor executor, Object... args) {
super(nThreads == 0 ? DEFAULT_EVENT_LOOP_THREADS : nThreads, executor, args);
} /**
* 6.1 我们看下静态代码块
* 作用:到这一步得出关键的一点:`如果初始化NioEventLoopGroup未指定线程数,默认是CPU核心数*2`。
*/
private static final int DEFAULT_EVENT_LOOP_THREADS; static {
DEFAULT_EVENT_LOOP_THREADS = Math.max(1, SystemPropertyUtil.getInt(
"io.netty.eventLoopThreads", NettyRuntime.availableProcessors() * 2))
} /**
* 7、继续调用父类 MultithreadEventLoopGroup 构造函数
* 作用:指定了一个EventExecutor的选择工厂DefaultEventExecutorChooserFactory,
* 此工厂主要是用于选择下一个可用的EventExecutor
*/
protected MultithreadEventExecutorGroup(int nThreads, Executor executor, Object... args) {
this(nThreads, executor, DefaultEventExecutorChooserFactory.INSTANCE, args);
} /**
* 8、继续调用父类 MultithreadEventLoopGroup 构造函数 这里就是核心代码 删除部分非核心代码
* 作用单独分析
*/
protected MultithreadEventExecutorGroup(int nThreads, Executor executor, EventExecutorChooserFactory chooserFactory, Object... args) { //1、
//executor校验非空, 如果为空就创建ThreadPerTaskExecutor, 该类实现了 Executor接口
// 这个executor 是用来执行线程池中的所有的线程,也就是所有的NioEventLoop,其实从
//NioEventLoop构造器中也可以知道,NioEventLoop构造器中都传入了executor这个参数。
if (executor == null) {
executor = new ThreadPerTaskExecutor(newDefaultThreadFactory());
} //2、
//这里的children数组, 其实就是线程池的核心实现,线程池中就是通过指定的线程数组来实现线程池;
//数组中每个元素其实就是一个EventLoop,EventLoop是EventExecutor的子接口。
children = new EventExecutor[nThreads]; //for循环实例化children数组,NioEventLoop对象
for (int i = 0; i < nThreads; i++) {
boolean success = false; //3、
//newChild(executor, args) 函数在NioEventLoopGroup类中实现了,
// 实质就是就是存入了一个 NIOEventLoop类实例
children[i] = newChild(executor, args);
success = true;
} //4、实例化线程工厂执行器选择器: 根据children获取选择器
chooser = chooserFactory.newChooser(children); //5、为每个EventLoop线程添加 线程终止监听器
final FutureListener<Object> terminationListener = new FutureListener<Object>() {
@Override
public void operationComplete(Future<Object> future) throws Exception {
if (terminatedChildren.incrementAndGet() == children.length) {
terminationFuture.setSuccess(null);
}
}
}; //6、将children 添加到对应的set集合中去重, 表示只可读。
Set<EventExecutor> childrenSet = new LinkedHashSet<EventExecutor>(children.length);
Collections.addAll(childrenSet, children);
readonlyChildren = Collections.unmodifiableSet(childrenSet);
}
} /**
* 8.3.1 我们再来看下 newChild(executor, args) 里的方法
* 我们可以看到 返回的就是一个 NioEventLoop
*/
@Override
protected EventLoop newChild(Executor executor, Object... args) throws Exception {
return new NioEventLoop(this, executor, (SelectorProvider) args[0],
((SelectStrategyFactory) args[1]).newSelectStrategy(), (RejectedExecutionHandler) args[2]);
}

我们再回顾总结一下:

1. NioEventLoopGroup初始化时未指定线程数,那么会使用默认线程数,即 `线程数 = CPU核心数 * 2`;
2. 每个NioEventLoopGroup对象内部都有一组可执行的`NioEventLoop数组`,其大小是 nThreads, 这样就构成了一个线程池, `一个NIOEventLoop可以理解成就是一个线程`。
3. 所有的NIOEventLoop线程是使用相同的 executor、SelectorProvider、SelectStrategyFactory、RejectedExecutionHandler以及是属于某一个
NIOEventLoopGroup的。这一点从 newChild(executor, args); 方法就可以看出:newChild()的实现是在NIOEventLoopGroup中实现的。
4. 当有IO事件来时,需要从线程池中选择一个线程出来执行,这时候的NioEventLoop选择策略是由GenericEventExecutorChooser实现的,并调用该类的next()方法。
5. 每个NioEventLoopGroup对象都有一个NioEventLoop选择器与之对应,其会根据NioEventLoop的个数,动态选择chooser(如果是2的幂次方,则按位运算,否则使用普通的轮询)

所以通过上面的分析,我们得出NioEventLoopGroup主要功能就是为了创建一定数量的NioEventLoop,而真正的重点就在NioEventLoop中,它是整个netty线程执行的关键。

【Netty】(3)—源码NioEventLoopGroup的更多相关文章

  1. 【Netty】(3)—源码NioEventLoopGroup

    netty(3)-源码NioEventLoopGroup 一.概念 NioEventLoopGroup对象可以理解为一个线程池,内部维护了一组线程,每个线程负责处理多个Channel上的事件,而一个C ...

  2. Netty 4源码解析:请求处理

    Netty 4源码解析:请求处理 通过之前<Netty 4源码解析:服务端启动>的分析,我们知道在最前端"扛压力"的是NioEventLoop.run()方法.我们指定 ...

  3. Netty 4源码解析:服务端启动

    Netty 4源码解析:服务端启动 1.基础知识 1.1 Netty 4示例 因为Netty 5还处于测试版,所以选择了目前比较稳定的Netty 4作为学习对象.而且5.0的变化也不像4.0这么大,好 ...

  4. Netty(6)源码-服务端与客户端创建

    原生的NIO类图使用有诸多不便,Netty向用户屏蔽了细节,在与用户交界处做了封装. 一.服务端创建时序图 步骤一:创建ServerBootstrap实例 ServerBootstrap是Netty服 ...

  5. 我为 Netty 贡献源码 | 且看 Netty 如何应对 TCP 连接的正常关闭,异常关闭,半关闭场景

    欢迎关注公众号:bin的技术小屋,本文图片加载不出来的话可查看公众号原文 本系列Netty源码解析文章基于 4.1.56.Final版本 写在前面..... 本文是笔者肉眼盯 Bug 系列的第三弹,前 ...

  6. Netty(7)源码-ByteBuf

    一.ByteBuf工作原理 1. ByteBuf是ByteBuffer的升级版: jdk中常用的是ByteBuffer,从功能角度上,ByteBuffer可以完全满足需要,但是有以下缺点: ByteB ...

  7. netty下载源码并导入idea

    netty源码导入eclipse会有一些兼容性问题,网上有解决方案,官方推荐idea,故此用idea. 拷贝git地址:https://github.com/netty/netty.git 使用git ...

  8. Netty ByteBuf源码分析

    Netty的ByteBuf是JDK中ByteBuffer的升级版,提供了NIO buffer和byte数组的抽象视图. ByteBuf的主要类集成关系: (图片来自Netty权威指南,图中有一个画错的 ...

  9. 【Netty】源码分析目录

    前言 为方便系统的学习Netty,特整理文章目录如下. [Netty]第一个Netty应用 [Netty]Netty核心组件介绍 [Netty]Netty传输 [Netty]Netty之ByteBuf ...

  10. netty UnpooledHeapByteBuf 源码分析

    UnpooledHeapByteBuf 是基于堆内存进行内存分配的字节缓冲区,没有基于对象池技术实现,这意味着每次I/O的读写都会创建一个新的UnpooledHeapByteBuf,频繁进行大块内存的 ...

随机推荐

  1. 本文是第一篇在GitHub仓库中撰写的.md格式的blog文件

    正文内容: 具体内容,只是未来测试,给出福利: 模板格式: title: 博文标题 description: 博文摘要 #多个标签请使用英文逗号分隔或使用数组语法 tags: 标签1, 标签2 #多个 ...

  2. 别再手动处理数据了!FastGPT 这个新功能让你提前下班

    大家好!今天给大家介绍 FastGPT 4.8.11 版本新增的一个超强节点 - [循环运行]节点.如果你经常需要处理大量数据,这个功能绝对能让你事半功功倍! 这个节点是干嘛的? 想象一下这个场景:你 ...

  3. 关于MNN的OPENCL和Vulkan支持

    关于MNN框架推理的时候,通过调用库当中结构体的内容,可以切换选择创建Session的具体配置.关于结构的描述见官方文档: 官方文档--创建Session CPU是编译的时候默认选择的配置方式,通过文 ...

  4. .NET 9 发布 性能提升、AI 支持与全方位改进

    前言 .NET 9 正式发布,这是迄今为止最高效.现代.安全.智能且高性能的 .NET 版本. 新版本凝聚了全球数千名开发者的共同努力,包含了数千项性能.安全性和功能性改进. 主要亮点 性能提升:全面 ...

  5. 【一步步开发AI运动小程序】四、小程序如何抽帧

    随着人工智能技术的不断发展,阿里体育等IT大厂,推出的"乐动力"."天天跳绳"AI运动APP,让云上运动会.线上运动会.健身打卡.AI体育指导等概念空前火热.那 ...

  6. Blocked aria-hidden on an element because its descendant retained focus.

    背景 vue 2.6.10 报错:Blocked aria-hidden on an element because its descendant retained focus. The focus ...

  7. Python基础:Python可变对象和不可变对象

    Python在heap中分配的对象分成两类:可变对象和不可变对象.所谓可变对象是指,对象的内容是可变的,例如list.而不可变的对象则相反,表示其内容不可变. 不可变对象:int,string,flo ...

  8. ScheduledThreadPoolExecutor与System#nanoTime

    一直流传着Timer使用的是绝对时间,ScheduledThreadPoolExecutor使用的是相对时间,那么ScheduledThreadPoolExecutor是如何实现相对时间的? 先看看S ...

  9. 原创单总线传输协议b2s (附全部verilog源码)

    一.b2s协议背景介绍 本单总线传输协议为精橙FPGA团队原创,含传送端(transmitter)和接收端(receiver)两部分,基于verilog语言,仅使用单个I/O口进行多位数据的传输,传输 ...

  10. 【Python自动化】selenium简单应用

    准备工作 查询浏览器版本 chrome://version/ 下载驱动 https://chromedriver.storage.googleapis.com/index.html 下载的驱动版本要和 ...