2021SC@SDUSC

文章目录

  • NioEventloopGroup
  • new NioEventLoopGroup();
  • 核心

NioEventloopGroup

类结构图:
很遗憾我的idea不是旗舰版,所以未能找到我现在正在研究的netty类图结构,只能先用这个看下

既然是叫事件执行器组,那就是和多个事件执行器工作相关的功能

注册和next

这里我的理解是多线程和事件执行器嘛,比较关键的是这个newChild,上边的也只是分析了解即可,这个new NioEventLoopGroup();话有一个返回值是NioEventLoop,大致也是组和个体的关系


可以看到在这个简单的服务器搭建的代码中,首先就是初始化了NioEventLoopGroup,首先可以ctrl跟进查看如何初始化

new NioEventLoopGroup();

为了方便,把接下来进入的方法和个人解析都放到代码块里。

public NioEventLoopGroup() {this(0);}
//一开始没什么好说的,不断地指向下一个方法,并且添加了新的参数,都是常见好理解的参数
//这里是线程,是指之后要创建的线程的数量,这个时候进入的是无参方法的路径,所以默认是0。
//如果是有参的构造方法,那这里就不是0了
public NioEventLoopGroup(int nThreads) {this(nThreads, (Executor) null);}
//executor:线程执行器,默认是 null
//这个属性的值会在后面创建 NioEventLoop 时,进行初始化。
//用户可以自定义executor,如果用户自定义实现,那么此时 executor 就不为 null,后面就也会再初始化。
public NioEventLoopGroup(int nThreads, Executor executor) {this(nThreads, executor, SelectorProvider.provider());}
//SelectorProvider.provider():通过它创建出一个 SelectorProvider 对象
//这个对象的作用就是创建多路复用器 Selector 和服务端 channel。
public NioEventLoopGroup(int nThreads, Executor executor, final SelectorProvider selectorProvider) {this(nThreads, executor, selectorProvider, DefaultSelectStrategyFactory.INSTANCE);}
//通过DefaultSelectStrategyFactory.INSTANCE 创建选择策略工厂,具体看后边。
//点开其方法可以看到INSTANCE由new DefaultSelectStrategyFactory();得到
public NioEventLoopGroup(int nThreads, Executor executor, final SelectorProvider selectorProvider,final SelectStrategyFactory selectStrategyFactory) {super(nThreads, executor, selectorProvider, selectStrategyFactory, RejectedExecutionHandlers.reject());}
//RejectedExecutionHandlers.reject():返回的是一个拒绝策略。
//当向线程池中添加任务时,如果线程池任务队列已满,这个时候任务就会被拒绝,此时线程池就会执行拒绝策略。
//reject()返回REJECT参数,它是由new RejectedExecutionHandler();获得。
protected MultithreadEventLoopGroup(int nThreads, Executor executor, Object... args) {super(nThreads == 0 ? DEFAULT_EVENT_LOOP_THREADS : nThreads, executor, args);}
static {DEFAULT_EVENT_LOOP_THREADS = Math.max(1, SystemPropertyUtil.getInt("io.netty.eventLoopThreads", NettyRuntime.availableProcessors() * 2));if (logger.isDebugEnabled()) {logger.debug("-Dio.netty.eventLoopThreads: {}", DEFAULT_EVENT_LOOP_THREADS);}}
//这里的DEFAULT_EVENT_LOOP_THREADS其实是一个static int值,通过static{}里可以看出这里如果参数是0
//会把nThreads改为CPU核数*2,我这里是16
//同时把后三个参数转为了args的Object数组protected MultithreadEventExecutorGroup(int nThreads, Executor executor, Object... args) {this(nThreads, executor, DefaultEventExecutorChooserFactory.INSTANCE, args);}
//通过DefaultEventExecutorChooserFactory.INSTANCE创建的是一个事件执行选择工厂
//INSTANCE 常量的值是通过new DefaultEventExecutorChooserFactory() 创建出来的对象。

核心

接下来就是最后的关键的代码实现,单独列出来,以便以后分析

protected MultithreadEventExecutorGroup(int nThreads, Executor executor,EventExecutorChooserFactory chooserFactory, Object... args) {if (nThreads <= 0) {throw new IllegalArgumentException(String.format("nThreads: %d (expected: > 0)", nThreads));}if (executor == null) {executor = new ThreadPerTaskExecutor(newDefaultThreadFactory());}children = new EventExecutor[nThreads];for (int i = 0; i < nThreads; i ++) {boolean success = false;try {children[i] = newChild(executor, args);success = true;} catch (Exception e) {// TODO: Think about if this is a good exception typethrow new IllegalStateException("failed to create a child event loop", e);} finally {if (!success) {for (int j = 0; j < i; j ++) {children[j].shutdownGracefully();}for (int j = 0; j < i; j ++) {EventExecutor e = children[j];try {while (!e.isTerminated()) {e.awaitTermination(Integer.MAX_VALUE, TimeUnit.SECONDS);}} catch (InterruptedException interrupted) {// Let the caller handle the interruption.Thread.currentThread().interrupt();break;}}}}}chooser = chooserFactory.newChooser(children);final FutureListener<Object> terminationListener = new FutureListener<Object>() {@Overridepublic void operationComplete(Future<Object> future) throws Exception {if (terminatedChildren.incrementAndGet() == children.length) {terminationFuture.setSuccess(null);}}};for (EventExecutor e: children) {e.terminationFuture().addListener(terminationListener);}Set<EventExecutor> childrenSet = new LinkedHashSet<EventExecutor>(children.length);Collections.addAll(childrenSet, children);readonlyChildren = Collections.unmodifiableSet(childrenSet);}

Nett源码剖析(1)NioEventloopGroup的创建2021SC@SDUSC相关推荐

  1. Nett源码剖析(2)NioEventloopGroup的创建2021SC@SDUSC

    2021SC@SDUSC 文章目录 new ThreadPerTaskExecutor(newDefaultThreadFactory()); newchild 上文说到这里是NioEventloop ...

  2. Nett源码剖析ServerBootstrap的设置2021SC@SDUSC

    2021SC@SDUSC 文章目录 bootstrap的设置剖析 其他 bootstrap的设置剖析 bootstrap.group(bossGroup, workerGroup)//设置两个线程组. ...

  3. Nett源码剖析注册通道2021SC@SDUSC

    2021SC@SDUSC 在绑定端口过程中,类initAndRegister里有注册通道方法ChannelFuture regFuture = config().group().register(ch ...

  4. ZBar源码分析——多线程部分代码分析 | 2021SC@SDUSC

    2021SC@SDUSC 目录 一.ZBar中的多线程 线程:cpu调度的最小单位 何为线程安全? 锁机制 二.ZBar中使用多线程的代码示例 Window线程的上锁与解锁 Vedio视频流的上锁与解 ...

  5. 4.2.10 Kafka源码剖析, 阅读环境搭建, broker启动流程, topic创建流程, Producer生产者流程, Consumer消费者流程,

    目录 4.1 Kafka源码剖析之源码阅读环境搭建 4.1.1 安装配置Gradle 4.1.2 Scala的安装和配置 4.1.3 Idea配置 4.1.4 源码操作 4.2 Kafka源码剖析之B ...

  6. 源码剖析 Netty 服务启动 NIO

    如果这个文章看不懂的话 , 建议反复阅读 Netty 与 Reactor 开篇立意.引用网友好的建议.看源码要针对性的看,最佳实践就是,带着明确的目的去看源码.抓主要问题,放弃小问题.主要的逻辑理解了 ...

  7. Netty服务器启动源码剖析

    Netty服务器启动源码剖析 文章目录 Netty服务器启动源码剖析 1.Netty服务器启动源码剖析 1.1.执行new NioEventLoopGroup()时发生了什么 1.1.1.NioEve ...

  8. JS魔法堂:mmDeferred源码剖析

    一.前言 avalon.js的影响力愈发强劲,而作为子模块之一的mmDeferred必然成为异步调用模式学习之旅的又一站呢!本文将记录我对mmDeferred的认识,若有纰漏请各位指正,谢谢.项目请见 ...

  9. Kafka源码剖析 —— 网络I/O篇 —— 浅析KafkaSelector

    为什么80%的码农都做不了架构师?>>>    ##NioSelector和KafkaSelector有什么区别? 先说结论,KafkaSelector(org.apache.kaf ...

最新文章

  1. C语言函数集(十四)
  2. 【转】MySQL的语句执行顺序
  3. git新手配置(ios环境)
  4. ASP.NET DROPDOWNLIST无刷新联动(中文URL参数处理)
  5. 约瑟夫环c语言程序完整版,约瑟夫环的c语言实现(代码已实现)
  6. 计算机相关论文摘要,计算机类论文摘要
  7. Android reckon 控制项目打包版本
  8. HDFS的设计目标是什么?
  9. PS制作华丽的紫色立体字
  10. 系统cpu主频查看设置
  11. 微信小程序-项目初始化
  12. 模拟器+Appium+Python抓取App内容
  13. 离散数学第6版25页41题
  14. 测绘程序设计实习 CSU
  15. 无纸化会议-安全初步
  16. 【Python网络爬虫】百度贴吧/豆瓣小组
  17. Duilib介绍-2
  18. 警告:[SetPropertiesRule]Setting property 'source' to xxx did not find a matching property.的消除
  19. 交通标志识别简单总结
  20. poi excel 常用操作 [冻结、合并、链接]

热门文章

  1. GitHub上的各大高校计算机学习资源
  2. 六、Python3自动化运维——电子邮件 smtplib模块
  3. 【计量经济学导论】01. 简单回归模型
  4. 手把手教你搭建入门级免费私有云盘NAS——基于syncthing——基础篇(树莓派、PC机)
  5. tensorflow/stream_executor/cuda/cuda_dnn.cc:378] Loaded runtime CuDNN library: 7301--2019.5.12
  6. 电子计算机应用是啥,第二代电子计算机使用的电子元件是什么
  7. SAP 生产订单/流程订单中日期的解释
  8. 【大屏】 amap + echarts 踩坑以及避免办法
  9. 读《互联网创业的四种玩家》有感
  10. knowledge-based systems 终于返回了意见——小修