第二节 netty源码分析之NioEventLoopGroup的构造函数分析

一、概述

上一节中,讲了netty的基本应用。这一节主要是根据上一节的应用代码一行行的进行netty的源码分析。上一节的应用代码的main函数里面的第一行是new NioEventLoopGroup(1),下面就分析一下NioEventLoopGroup 的构造方法。

二、NioEventLoopGroup

从类的名字翻译出来 Nio事件轮训组。讲道理的话,它跟线程组是一个意思,都是有一组线程在做同样的申请,就是轮训Channel的IO事件。

先看NioEventLoopGroup 的构造方法。

public NioEventLoopGroup(int nThreads) {
    this(nThreads, (Executor) null);
}

继续找构造方法会找到父类 MultithreadEventLoopGroup的构造方法。

protected MultithreadEventLoopGroup(int nThreads, ThreadFactory threadFactory, Object... args) {
    //nThreads 不传就是0 如果是0的话,会取CPU核数 * 2 作为默认线程数
    //threadFactory 为null
    // args里面有 
    //	 SelectorProvider.provider()、
    //   DefaultSelectStrategyFactory.INSTANCE===》new DefaultSelectStrategyFactory()
    //   RejectedExecutionHandlers.reject() ===》 new RejectedExecutionHandler()
    super(nThreads == 0 ? DEFAULT_EVENT_LOOP_THREADS : nThreads, threadFactory, args);
}

继续找构造方法会找到MultithreadEventExecutorGroup的构造方法

protected MultithreadEventExecutorGroup(int nThreads, Executor executor, Object... args) {
    // nThreads 不传就是0 如果是0的话,会取CPU核数 * 2 作为默认线程数
    //executor 为null
    //新增了一个参数 DefaultEventExecutorChooserFactory
    // args里面有 
    //	 SelectorProvider.provider()、
    //   DefaultSelectStrategyFactory.INSTANCE===》new DefaultSelectStrategyFactory()
    //   RejectedExecutionHandlers.reject() ===》 new RejectedExecutionHandler()
    this(nThreads, executor, DefaultEventExecutorChooserFactory.INSTANCE, args);
}

继续往下找构造方法

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) {
        //ThreadPerTaskExecutor 这个主要是用来新建线程来执行任务的,
        //newDefaultThreadFactory()=线程工厂  专门创建线程的
        //newDefaultThreadFactory()调用的是 MultithreadEventLoopGroup.newDefaultThreadFactory()
        //最终创建的是DefaultThreadFactory,他实现了继承自jdk的ThreadFactory
        executor = new ThreadPerTaskExecutor(newDefaultThreadFactory());
    }

    //children 就是标识boss线程组或者work线程组一共有几个线程,
    //从代码分析上来看我们没有指定executor 所以线程的创建默认由ThreadPerTaskExecutor负责
    children = new EventExecutor[nThreads];

    for (int i = 0; i < nThreads; i ++) {
        //出现异常标识 
        boolean success = false;
        try {
            //创建nThreads个nioEventLoop保存到children数组中
            //newChild()是个抽象方法,可以看到实现类NioEventLoopGroup实现了此方法
            //创建NioEventLoop对象
            children[i] = newChild(executor, args);
            success = true;
        } catch (Exception e) {
            // TODO: Think about if this is a good exception type
            throw 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是NioEventLoop的对象数组 
    //chooser=GenericEventExecutorChooser/PowerOfTwoEventExecutorChooser
    //chooser的意思是我要选择从NioEventLoop[]选一个线程出来工作的,是通过什么规则选的。
    chooser = chooserFactory.newChooser(children);

    final FutureListener<Object> terminationListener = new FutureListener<Object>() {
        @Override
        public 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);
}

三、总结

​ new NioEventLoopGroup()的构造方法分析,主要还是找到了父类MultithreadEventExecutorGroup的构造方法。每经过一层构造方法调用,会给一些默认的参数值。最终的参数值有

​ 1、 nThreads 不传就是0 如果是0的话,会取CPU核数 * 2 作为默认线程数,用来做boss线程组/woker线程组的线程数。

​ 2、threadFactory 线程工厂创建使用的 因为没有指定所以 为null 最终使用的是netty实现的默认线程工厂。

​ 3、executor 因为没有指定所以 为null,使用的是netty实现的ThreadPerTaskExecutor(),并且把threadFactory 做为构造参数,主要负责通过threadFactory 创建线程。

​ 4、SelectorProvider.provider() jdk原生的 SelectorProvider ,用来创建ServerSocketChannel。

​ 5、EventExecutorChooserFactory 从NioEventLoop[]选一个线程出来工作的,是通过什么规则选的。 使用的netty实现的 DefaultEventExecutorChooserFactory

​ 6、DefaultSelectStrategyFactory 选择策略工厂,DefaultSelectStrategyFactory.INSTANCE===》new DefaultSelectStrategyFactory()

​ 7、RejectedExecutionHandler 拒绝策略 RejectedExecutionHandlers.reject() ===》 new RejectedExecutionHandler()

以上参数的使用地方有:
NioEventLoopGroup 继承 MultithreadEventExecutorGroup ,
children = new EventExecutor[nThreads];
chooser = chooserFactory.newChooser(children);
children[i] = newChild(executor, args);
主要核心代码为 children[i] = newChild(executor, args); 后续再继续分析newChild里面做了什么事情。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值