netty源码分析之-NioEventLoopGroup与线程设定(1)
来源:互联网 发布:mac qq同时登陆 编辑:程序博客网 时间:2024/06/05 19:30
对于服务端代码:
EventLoopGroup bossGroup = new NioEventLoopGroup();EventLoopGroup workerGroup = new NioEventLoopGroup();
bossGroup的作用是接受客户端的连接,连接的数据到来之后,bossGroup不会作任何处理,而是交给workerGroup来完成处理逻辑。NioEventLoopGroup称为异步事件循环组,底层实际上就是死循环,不停的去侦测底层输入输出的事件。可以传入第一个参数表示线程的数量。
EventLoopGroup接口中只要方法:
EventLoop next(); 1 ChannelFuture register(Channel channel); 2 ChannelFuture register(ChannelPromise promise); 3
- 返回下一个EventLoop供使用
- 注册一个channel(连接)到EventLoop,并返回一个异步ChannelFuture,当channel注册完成之后ChannelFuture将会被通知。也就是调用这个方法会立即返回,但是可以通过返回的ChannelFuture来异步获取相应的结果数据
- 传入的ChannelPromise本身就继承了ChannelFuture,因此本身就是一个异步的对象,并且会持有一个Channel的引用,也就是可以通过调用本身的异步方法来获取注册完成之后的数据
对于NioEventLoopGroup,这个类是我们通常会使用的一个对EventLoopGroup的实现。NioEventLoopGroup用作基于channel的Nio中的Selector相关操作。
对于NioEventLoopGroup()默认的不传参数的实例化,一步步的追溯将会发现最终会调用到该类中的如下方法:
public NioEventLoopGroup(int nThreads, Executor executor, final SelectorProvider selectorProvider,final SelectStrategyFactory selectStrategyFactory) { super(nThreads,executor,selectorProvider,selectStrategyFactory,RejectedExecutionHandlers.reject());}
对于该方法的参数都有对应的默认值,nThreads会是0表示线程数,executor为null,selectorProvider为系统底层相关实现返回nio中的SelectorProvider,selectStrategyFactory默认会有相应的策略,包括最后一个拒绝的默认方式执行处理器。
NioEventLoopGroup extends MultithreadEventLoopGroup
最终会调用父类MultithreadEventLoopGroup中的相应方法:
protected MultithreadEventLoopGroup(int nThreads, Executor executor, Object... args) { super(nThreads == 0 ? DEFAULT_EVENT_LOOP_THREADS : nThreads, executor, args); }
对于线程数DEFAULT_EVENT_LOOP_THREADS:
private static final int DEFAULT_EVENT_LOOP_THREADS; 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); } }
该方法会调用系统运行时的方法,将操作系统的核心数(超核心技术会*2),然后乘以2作为默认的线程数。
public abstract class MultithreadEventLoopGroup extends MultithreadEventExecutorGroup implements EventLoopGroup
然后调用父类MultithreadEventExecutorGroup中的下列方法:
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; ...
最终可以分析出EventLoopGroup主要做的是一些准备工作,一些变量的初始化以及系统资源的一些准备工作,并没有对线程的启动之类的代码。
- netty源码分析之-NioEventLoopGroup与线程设定(1)
- Netty源码分析:NioEventLoopGroup
- Netty学习之旅------再谈线程模型之源码分析NioEventLoopGroup、SingleThreadEventExecutor
- 【Netty4.X】Netty源码分析之NioEventLoopGroup(五)
- Netty之Reactor线程1 -- NioEventLoopGroup(Reactor线程)
- Netty源码分析系列1——NIOEventLoopGroup的创建
- netty 启动分析 NioEventLoopGroup
- Netty源码学习——EventLoopGroup原理:NioEventLoopGroup分析
- netty实战之百万级流量NioEventLoopGroup线程数配置
- netty NioEventLoopGroup 线程名称设置
- netty源码分析 之十三 线程模型
- Netty源码分析之Reactor线程模型
- Netty学习之旅------源码分析Netty线程本地分配机制与PooledByteBuf线程级对象池原理分析
- netty源码分析之-Channel与ChannelPipeline详解(6)
- netty源码分析之-ChannelHandler与ChannelContext详解(7)
- [netty源码分析]--EventLoopGroup与EventLoop 分析netty的线程模型
- netty(十)源码分析之ByteBuf
- netty(十三)源码分析之Channel
- 多线程概述 -- 转
- C语言小项目—注释转换( C注释转换为C++注释 )
- BZOJ1717:[Usaco2006 Dec]Milk Patterns 产奶的模式
- 第四周项目5
- 22. Generate Parentheses
- netty源码分析之-NioEventLoopGroup与线程设定(1)
- 八皇后
- [cnblogs镜像] MySQL查询结果保存到本地
- 链表题--难点
- codewars解题笔记 —— 字符串的筛选和拼接
- Android 常见的adb命令
- js基础入门,获取元素的方式,三个对象模型
- Our Journey of Dalian Ends (最小费用最大流)
- [cnblogs镜像] Python2以及Python3中的除法