Netty NioEventLoop创建逻辑

NioEventLoop

NioEventLoop 创建

new NioEventLoopGroup 线程组,默认 2*cpu
    new ThreadPerTaskExecutor() 线程创建器,创建 NioEventLoop 底层的线程
    循环调用 newChild() 创建 NioEventLoop
    chooserFactory.newChooser() 线程选择器,为每个新链接分配 NioEventLoop 线程

ThreadPerTaskExecutor

每次只需任务时都会创建一个线程实体

NioEventLoop 线程命名规则 nioEventLoop-1-xx : -1代表第几个 NioEventLoopGroup

DefaultThreadFactory

MultithreadEventExecutorGroup#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()); // 创建 ThreadPerTaskExecutor
    }

    children = new EventExecutor[nThreads];

    for (int i = 0; i < nThreads; i ++) {
        boolean success = false;
        try {
            children[i] = newChild(executor, args); // 创建线程池中的线程,其实创建的是 NioEventLoop 实例
            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) { // 只要有一个 child 没有实例化成功,就会走这里失败处理逻辑
                for (int j = 0; j < i; j ++) { // 把已经成功实例化的"线程" shutdown,shutdown 是异步操作
                    children[j].shutdownGracefully();
                }

                for (int j = 0; j < i; j ++) { // 等待这些线程成功 shutdown
                    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;
                    }
                }
            }
        }
    }
    // 设置 chooserFactory,用来实现从线程池中选择一个线程的选择策略
    chooser = chooserFactory.newChooser(children);
    // 设置一个 Listener 用来监听该线程池的 termination 事件
    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) {// 给池中每一个线程都设置这个 listener,当监听到所有线程都 terminate 以后,这个线程池就算真正的 terminate 了。
        e.terminationFuture().addListener(terminationListener);
    }

    Set<EventExecutor> childrenSet = new LinkedHashSet<EventExecutor>(children.length);
    Collections.addAll(childrenSet, children);
    readonlyChildren = Collections.unmodifiableSet(childrenSet);
}

newChild 逻辑

保存线程执行器 ThreadPerTaskExecutor
创建一个 MpscQueue
创建一个 selector

方法: io.netty.channel.nio.NioEventLoopGroup#newChild

newChild()
    NioEventLoopGroup.newChild()
        NioEventLoop构造方法:创建selector,关联NioEventLoop
        newTaskQueue()创建MPSC队列

创建 chooser

// 设置 chooserFactory,用来实现从线程池中选择一个线程的选择策略
chooser = chooserFactory.newChooser(children);

方法: io.netty.util.concurrent.DefaultEventExecutorChooserFactory#newChooser

public EventExecutorChooser newChooser(EventExecutor[] executors) {// 不同的线程池选择策略,选择效率会高一点
    if (isPowerOfTwo(executors.length)) {// 如果线程池的线程数量是 2^n
        return new PowerOfTwoEventExecutorChooser(executors);
    } else {
        return new GenericEventExecutorChooser(executors);// 如果不是,用取模的方式
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

FlyingZCC

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值