Netty组件之Channel注册
前言 本文将分析EventLoopGroup初始化、EventLoop的选择策略以及Channel是如何通过EventLoop注册到Selector上的。 一、EventLoopGroup类图概览 在客户端示例代码中的中实例化了NioEventLoopGroup,接下来分析下该实例化过程。 EventLoopGroup workerGroup = new NioEventLoopGroup();Bootstrap b = new Bootstrap();b.group(workerGroup); 从以下类图结构io.netty.util.concurrent.AbstractEventExecutorGroup分支主要负责多线程任务的处理;io.netty.channel.EventLoopGroup分支主要负责Channel相关的注册。MultithreadEventExecutorGroup与MultithreadEventLoopGroup分别继承和实现了上面AbstractEventExecutorGroup和EventLoopGroup,将其负责的功能进行融合。 二、构造函数解读 构造函数nThreads:eventLoopThreads线程数量,默认值0时取CPU核数的2倍,可以通过参数io.netty.eventLoopThreads指定Executor:默认ThreadPerTaskExecutorSelectorProvider默认SelectorProvider.provider(),用于开启Selector和ChannelSelectStrategyFactory:SelectStrategy工厂类,默认DefaultSelectStrategyFactoryEventExecutorChooserFactory:EventExecutor选择器,默认为DefaultEventExecutorChooserFactory 三、初始化EventExecutor数组 代码解读EventExecutor[] children:数组大小为nThreads,默认为CPU核数乘以2。EventExecutor继承了EventExecutorGroup本质上为线程框架类Executorchildren[i]:数据元素为EventLoop,本示例中为NioEventLoop。 NioEventLoop类图 NioEventLoop继承了SingleThreadEventLoop,SingleThreadEventLoop同时继承和实现了EventExecutor和EventLoop。即:NioEventLoop拥有了线程类框架处理多线程任务的能力和处理Channel能力。 备注:本文中EventExecutor数组children的元素为NioEventLoop,NioEventLoop同时拥有线程框架能力和Channel注册等处理能力。 四、EventExecutor选择器 第三部分对EventExecutor[] children进行初始化分析,然在使用时如何选择其中一个元素呢?在初始化过程中有以下一行代码,用于初始化EventExecutorChooser。 chooser = chooserFactory.newChooser(children); EventExecutorChooser类图结构 选择策略 @1 如果数组长度是2的幂次方,选择PowerOfTwoEventExecutorChooser,在选取EventExecutor时使用executors[idx.getAndIncrement() & executors.length - 1] @2 如果数组长度不是2的幂次方,选择GenericEventExecutorChooser,executors[Math.abs(idx.getAndIncrement() % executors.length)]。 五、Channel注册 Channel注册入口 选择EventLoop本文为NioEventLoop 绑定Channel到EventExecutor通过DefaultChannelPromise绑定Channel到EventExecutor(NioEventLoop) 将Channel注册到Selector 作者丨梁勇来源丨瓜农老梁(ID:gh_01130ae30a83)欢迎关注公众号“瓜农老梁” 「瓜农老梁学习同行」 本文分享自微信公众号 - 瓜农老梁(gh_01130ae30a83)。如有侵权,请联系 support@oschina.cn 删除。本文参与“OSC源创计划”,欢迎正在阅读的你也加入,一起分享。