初始化"/>
Netty源码解析(一)初始化
Netty运行整体流程:
以后的讲究都会按着这幅图来理解
1.对NioEventLoopGroup的整体流程
从第一行代码分析
EventLoopGroup bossGroup=new NioEventLoopGroup(1);//cpu核心数*2
EventLoopGroup workerGroup=new NioEventLoopGroup(); //cpu核心数*2
看NioEventLoopGroup的构造方法
public NioEventLoopGroup(int nThreads) {//这里给Executor赋值为nullthis(nThreads, (Executor) null);}
继续往里面跟
public NioEventLoopGroup(int nThreads, Executor executor) {//executor默认为null//ServerSocketChannel 就是通过ServerSocketChannel.open()==》SelectorProvider.provider().openServerSocketChannel()创建的this(nThreads, executor, SelectorProvider.provider());}
这里可以把SelectorProvider.provider()理解为打开ServerSocketChannel的类
继续跟
public NioEventLoopGroup(int nThreads, Executor executor, final SelectorProvider selectorProvider) {//nThreads默认为零//executor默认为null//ServerSocketChannel 就是通过ServerSocketChannel.open()==》SelectorProvider.provider().openServerSocketChannel()创建的//DefaultSelectStrategyFactory.INSTANCE===》new DefaultSelectStrategyFactory() 默认选择策略工厂this(nThreads, executor, selectorProvider, DefaultSelectStrategyFactory.INSTANCE);}
这里要注意的是选择策略采用的是默认策略工厂,就是选择NioEventLoop的方法
继续跟
public NioEventLoopGroup(int nThreads, Executor executor, final SelectorProvider selectorProvider,final SelectStrategyFactory selectStrategyFactory) {//nThreads默认为零//executor默认为null//ServerSocketChannel 就是通过ServerSocketChannel.open()==》SelectorProvider.provider().openServerSocketChannel()创建的//DefaultSelectStrategyFactory.INSTANCE===》new DefaultSelectStrategyFactory()//线程池的拒绝策略,是指当任务添加到线程池中被拒绝,而采取的处理措施。// 当任务添加到线程池中之所以被拒绝,可能是由于:第一,线程池异常关闭。第二,任务数量超过线程池的最大限制。//RejectedExecutionHandlers.reject() ===》 new RejectedExecutionHandler() ===>丢弃任务并抛出RejectedExecutionException异常。super(nThreads, executor, selectorProvider, selectStrategyFactory, RejectedExecutionHandlers.reject());}
然后调用父类构造方法
protected MultithreadEventLoopGroup(int nThreads, Executor executor, Object... args) {//nThreads默认为零//executor默认为null//SelectorProvider ServerSocketChannel就是通过ServerSocketChannel.open()==》SelectorProvider.provider().openServerSocketChannel()创建的//DefaultSelectStrategyFactory.INSTANCE===》new DefaultSelectStrategyFactory()//RejectedExecutionHandlers.reject() ===》 new RejectedExecutionHandler()//nThreads如果不传默认是0 如果是0的话 就获取CPU核数的两倍 DEFAULT_EVENT_LOOP_THREADS==CPU核数的两倍super(nThreads == 0 ? DEFAULT_EVENT_LOOP_THREADS : nThreads, executor, args);}
这里也看出MultithreadEventLoopGroup是NioEventLoopGroup的父类
这里是确定开启线程的个数
static {DEFAULT_EVENT_LOOP_THREADS = Math.max(1, SystemPropertyUtil.getInt(//NettyRuntime.availableProcessors()是CPU核数"ioty.eventLoopThreads", NettyRuntime.availableProcessors() * 2));if (logger.isDebugEnabled()) {logger.debug("-Dioty.eventLoopThreads: {}", DEFAULT_EVENT_LOOP_THREADS);}}
继续跟
调用super,说明MultithreadEventExecutorGroup是MultithreadEventLoopGroup的父类
//nThreads如果不传默认是0 如果是0的话 就获取CPU核数的两倍 DEFAULT_EVENT_LOOP_THREADS==CPU核数的两倍//executor默认为null//chooserFactory=new DefaultEventExecutorChooserFactory() 默认 事件执行策略工厂//args参数如下//SelectorProvider ServerSocketChannel就是通过ServerSocketChannel.open()==》SelectorProvider.provider().openServerSocketChannel()创建的//DefaultSelectStrategyFactory.INSTANCE===》new DefaultSelectStrategyFactory()//RejectedExecutionHandlers.reject() ===》 new RejectedExecutionHandler()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) {//newDefaultThreadFactory()=线程工厂 专门创建线程的//newDefaultThreadFactory()调用的是 MultithreadEventLoopGroup.newDefaultThreadFactory()//最终创建的是DefaultThreadFactory,他实现了继承自jdk的ThreadFactoryexecutor = new ThreadPerTaskExecutor(newDefaultThreadFactory());}//nThreads如果不传默认是0 如果是0的话 就获取CPU核数的两倍 DEFAULT_EVENT_LOOP_THREADS==CPU核数的两倍children = new EventExecutor[nThreads];for (int i = 0; i < nThreads; i ++) {//出现异常标识boolean success = false;try {//创建nThreads个nioEventLoop保存到children数组中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;}}}}}//children是 new NioEventLoop() 的对象数组//chooser=GenericEventExecutorChooser/PowerOfTwoEventExecutorChooserchooser = 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);}//复制一份children 只读的Set<EventExecutor> childrenSet = new LinkedHashSet<EventExecutor>(children.length);Collections.addAll(childrenSet, children);readonlyChildren = Collections.unmodifiableSet(childrenSet);}
到这里,NioEventLoopGroup创建完成
继续我们的代码,下一步就是创建ServerBootStarp,设置参数来绑定端口启动服务
ServerBootstrap serverBootstrap=new ServerBootstrap();
可以看到属性和构造方法
public ServerBootstrap() { },说明构造方法什么都没设置
ServerBootstrap 的父类是AbstractBootstrap
我们再来看两个类的属性
ServerBootstrap :
AbstractBootstrap:
可以看到ServerBootstrap 中存放的都是客户端(NioSocketChannel)相关的参数,AbstractBootstrap存放的都是服务端(NioServerSocketChannel)的相关参数,而serverBootStarp.group(。。。)的方法都是简单的设置参数,这里不再做详细说明
到这里初始化工作基本完成
下一篇研究bind方法
更多推荐
Netty源码解析(一)初始化
发布评论