netty之NioEventLoop和NioEventLoopGroup

类结构图

NioEventLoopGroup

MultithreadEventLoopGroup NioEventLoopGroup MultithreadEventExecutorGroup AbstractEventExecutorGroup <<interface>> EventExecutorGroup <<interface>> EventLoopGroup

NioEventLoop

<<interface>> EventExecutor AbstractEventExecutor AbstractScheduledEventExecutor SingleThreadEventExecutor SingleThreadEventLoop NioEventLoop <<interface>> EventLoop

初始化

线程池创建是通过MultithreadEventExecutorGroup构造函数,创建线程是通过DefaultThreadFactory

首先创建EventExecutor的数组

cpp 复制代码
children = new EventExecutor[nThreads];

NioEventLoopGroup的newChild创建具体的eventloop即NioEventLoop

SingleThreadEventExecutor在重写了线程池的execute方法时,其会创建线程,执行SingleThreadEventExecutor.this.run()抽象方法

同时会初始化EventExecutorChooser, 有两种类型

  • PowerOfTwoEventExecutorChooser
  • GenericEventExecutorChooser

初始化SelectorProvider,默认使用sun.nio.ch.DefaultSelectorProvider.create()

java 复制代码
public static SelectorProvider provider() {
        synchronized (lock) {
            if (provider != null)
                return provider;
            return AccessController.doPrivileged(
                new PrivilegedAction<SelectorProvider>() {
                    public SelectorProvider run() {
                            if (loadProviderFromProperty())
                                return provider;
                            if (loadProviderAsService())
                                return provider;
                            provider = sun.nio.ch.DefaultSelectorProvider.create();
                            return provider;
                        }
                    });
        }
    }

初始化DefaultSelectStrategy,默认使用SelectStrategy.SELECT

创建NioEventLoop传递的executor参数为ThreadPerTaskExecutor

相关推荐
WJ.Polar15 分钟前
final、单例类、枚举类、抽象类、接口
java·开发语言
江湖有缘25 分钟前
Docker实战 | 使用Docker部署Imagor Studio图片编辑工具
java·docker·eureka
zhangjw3425 分钟前
1-3 PostgreSQL 架构模型:实例、数据库、Schema 模式、表空间完整概念
数据库·postgresql·架构
小羊没烦恼!33 分钟前
C#综合揭秘——Entity Framework 并发处理详解
java·开发语言·前端·javascript·数据库·mysql·c#
Databend37 分钟前
周年庆结束了,湖仓为什么还在高消费?
大数据·数据库·agent
笃行3501 小时前
数据集成软件里最贵的一行字,叫“用同步工具做增量同步“
数据库
吴声子夜歌1 小时前
Docker入门与实战——Etcd(高可用的键值数据库)
数据库·docker·etcd
刘 大 望1 小时前
Agent 的规划与执行范式:Plan-Execute-Replan 与 ReAct
java·python·ai·chatgpt·langchain·ai编程
qq_白羊座1 小时前
Hologres / PostgreSQL jsonb 常用函数 & 操作符对照表
数据库·postgresql
千桐科技1 小时前
qData 数据中台专业版功能解析:告别 Excel 与 SQL 拼凑,打通系统合并的可追踪数据链路
数据库·sql·excel·qdata