【Netty】(3)—源码NioEventLoopGroup
netty(3)—源码NioEventLoopGroup
一、概念
NioEventLoopGroup对象可以理解为一个线程池,内部维护了一组线程,每个线程负责处理多个Channel上的事件,而一个Channel只对应于一个线程,这样可以回避多线程下的数据同步问题。
我们先回顾下 上篇博客的服务器代码
// 定义一对线程组
// 主线程组, 用于接受客户端的连接,但是不做任何处理,跟老板一样,不做事
EventLoopGroup bossGroup = new NioEventLoopGroup();
// 从线程组, 老板线程组会把任务丢给他,让手下线程组去做任务
EventLoopGroup workerGroup = new NioEventLoopGroup();
// netty服务器的创建, 辅助工具类,用于服务器通道的一系列配置
ServerBootstrap serverBootstrap = new ServerBootstrap();
serverBootstrap.group(bossGroup, workerGroup) //绑定两个线程组
//省略......
职责:
- 作为服务端
Acceptor 线程,负责处理客户端的请求接入。 - 作为客户端
Connector 线程,负责注册监听连接操作位,用于判断异步连接结果。 - 作为
IO 线程,监听网络读操作位,负责从 SocketChannel 中读取报文。 - 作为 IO 线程,负责向 SocketChannel 写入报文发送给对方,如果发生写半包,会自动注册监听写事件,用 于后续继续发送半包数据,直到数据全部发送完成。
- 作为
定时任务线程,可以执行定时任务,例如链路空闲检测和发送心跳消息等。 - 作为线程执行器可以执行普通的任务线程(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());
}
经过上面一系列的构造方法调用,此时参数值对应如下:
- nThreads: 0
- executor: null
- selectorProvider: SelectorProvider.provider()
- selectStrategyFactory: DefaultSelectStrategyFactory.INSTANCE
- 以及指定了拒绝策略: 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的更多相关文章
- 【Netty】(3)—源码NioEventLoopGroup
netty(3)-源码NioEventLoopGroup 一.概念 NioEventLoopGroup对象可以理解为一个线程池,内部维护了一组线程,每个线程负责处理多个Channel上的事件,而一个C ...
- Netty 4源码解析:请求处理
Netty 4源码解析:请求处理 通过之前<Netty 4源码解析:服务端启动>的分析,我们知道在最前端"扛压力"的是NioEventLoop.run()方法.我们指定 ...
- Netty 4源码解析:服务端启动
Netty 4源码解析:服务端启动 1.基础知识 1.1 Netty 4示例 因为Netty 5还处于测试版,所以选择了目前比较稳定的Netty 4作为学习对象.而且5.0的变化也不像4.0这么大,好 ...
- Netty(6)源码-服务端与客户端创建
原生的NIO类图使用有诸多不便,Netty向用户屏蔽了细节,在与用户交界处做了封装. 一.服务端创建时序图 步骤一:创建ServerBootstrap实例 ServerBootstrap是Netty服 ...
- 我为 Netty 贡献源码 | 且看 Netty 如何应对 TCP 连接的正常关闭,异常关闭,半关闭场景
欢迎关注公众号:bin的技术小屋,本文图片加载不出来的话可查看公众号原文 本系列Netty源码解析文章基于 4.1.56.Final版本 写在前面..... 本文是笔者肉眼盯 Bug 系列的第三弹,前 ...
- Netty(7)源码-ByteBuf
一.ByteBuf工作原理 1. ByteBuf是ByteBuffer的升级版: jdk中常用的是ByteBuffer,从功能角度上,ByteBuffer可以完全满足需要,但是有以下缺点: ByteB ...
- netty下载源码并导入idea
netty源码导入eclipse会有一些兼容性问题,网上有解决方案,官方推荐idea,故此用idea. 拷贝git地址:https://github.com/netty/netty.git 使用git ...
- Netty ByteBuf源码分析
Netty的ByteBuf是JDK中ByteBuffer的升级版,提供了NIO buffer和byte数组的抽象视图. ByteBuf的主要类集成关系: (图片来自Netty权威指南,图中有一个画错的 ...
- 【Netty】源码分析目录
前言 为方便系统的学习Netty,特整理文章目录如下. [Netty]第一个Netty应用 [Netty]Netty核心组件介绍 [Netty]Netty传输 [Netty]Netty之ByteBuf ...
- netty UnpooledHeapByteBuf 源码分析
UnpooledHeapByteBuf 是基于堆内存进行内存分配的字节缓冲区,没有基于对象池技术实现,这意味着每次I/O的读写都会创建一个新的UnpooledHeapByteBuf,频繁进行大块内存的 ...
随机推荐
- C++ STL 容器简介
1.总述 C++ STL(Standard Template Library)是 C++ 标准库的一部分,包括了许多数据结构的实现,提供了许多好用的轮子,同时,其设计思想也非常值得学习.其中,容器是 ...
- Nuxt.js 应用中的 vite:extend 事件钩子详解
title: Nuxt.js 应用中的 vite:extend 事件钩子详解 date: 2024/11/11 updated: 2024/11/11 author: cmdragon excerpt ...
- 鸿蒙开发Hvigor任务简介
编译构建工具DevEco Hvigor(以下简称Hvigor)是一款基于TS实现的构建任务编排工具,主要提供任务管理机制,包括任务注册编排.工程模型管理.配置管理等关键能力,提供专用于构建和测试应用的 ...
- Apache Log4j2远程命令执行漏洞复现
目录 漏洞原理 复现 漏洞修复 Apache Log4j2 是一个基于Java的日志记录工具,被广泛应用于业务系统开发,开发者可以利用该工具将程序的输入输出信息进行日志记录.Log4j2 远程代码执行 ...
- ES6中 Json、String、Map、Object 之间的转换
/** *字符串转json * */ static stringToJson(data){ return JSON.parse(data); } /** *json转字符串 */ static jso ...
- Java 并发编程实战学习笔记——CountDownLatch的使用
public class CountDownLatch extends Object 一个同步辅助类,在完成一组正在其他线程中执行的操作之前,它允许一个或多个线程一直等待. 用给定的计数 初始化 Co ...
- 探索 TypeScript 编程的利器:ts-morph 入门与实践
我们是袋鼠云数栈 UED 团队,致力于打造优秀的一站式数据中台产品.我们始终保持工匠精神,探索前端道路,为社区积累并传播经验价值. 本文作者:贝儿 背景 在开发 web IDE 中生成代码大纲的功能时 ...
- Node.js 模拟Apache服务器
1.知识必备 (1)当服务器响应不同文件类型时,需要设置响应报文头,让浏览器选择相应的编码解析数据. 常用对照表HTTP Mime-type: https://tool.oschina.net/com ...
- 原创单总线传输协议b2s (附全部verilog源码)
一.b2s协议背景介绍 本单总线传输协议为精橙FPGA团队原创,含传送端(transmitter)和接收端(receiver)两部分,基于verilog语言,仅使用单个I/O口进行多位数据的传输,传输 ...
- 题解 ICPC 2019 SH 区域赛 F 树上简单问题
题解 ICPC 2019 SH 区域赛 F 树上简单问题 CF的Gym里没找着 牛客的题目链接 首先这个题多测非常SB, 每次都要清空, 需要特别注意. 树剖应该都会吧, Defad之后也会发博客讲解 ...