【Netty专题】Netty源码剖析_NioEventLoopGroup组件

用过Netty的都知道,在Netty启动的时候我们需要设置两个线程组,一个叫做Boss,一个叫做Worker,那么两个其实本质都是_NioEventLoopGroup线程组对象。

接下来我们来分析下_NioEventLoopGroup都做了哪些事情?

  • 创建一定数量的NioEventLoop线程组并初始化
  • 创建线程选择器chooser,当获取线程时,通过选择器来获取。
  • 创建线程工厂并构建线程执行器。

NioEventLoopGroup的结构图:
在这里插入图片描述
可以看到_NioEventLoopGroup继承MultithreadEventLoopGroup,所以在初始化时,会先执行父类MultithreadEventLoopGroup的构造方法,那么在此时会指定生成多少NioEventLoop线程,默认为CPU核数的两倍,代码如下:

public abstract class MultithreadEventLoopGroup extends MultithreadEventExecutorGroup implements EventLoopGroup {

    private static final InternalLogger logger = InternalLoggerFactory.getInstance(MultithreadEventLoopGroup.class);

    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);
        }
    }

其中线程组的生产分为两部分

  • 第一部分:创建一定数量的EventExecutor数组
  • 第二部分:通过调用子类的newChild()方法完成这些EventExecutor数组的初始化。

我们来看一下MultithreadEventExecutorGroup的构造方法:

 /**
     * Create a new instance.
     *
     * @param nThreads          the number of threads that will be used by this instance.
     * @param executor          the Executor to use, or {@code null} if the default should be used.
     * @param chooserFactory    the {@link EventExecutorChooserFactory} to use.
     * @param args              arguments which will passed to each {@link #newChild(Executor, Object...)} call
     */
    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());
        }

        // 根据线程数构建EventLoopGroup数组
        children = new EventExecutor[nThreads];

        for (int i = 0; i < nThreads; i ++) {
            boolean success = false;
            try {
                // 初始化线程数组中的线程,由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;
                        }
                    }
                }
            }
        }

        // 根据线程数创建选择器,选择器主要适用于next方法
        // chooser用来计算下一个选择的线程组的下标index
        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);
                }
            }
        };

        // 为每个 EventLoop 线程添加线程终止监听器
        for (EventExecutor e: children) {
            e.terminationFuture().addListener(terminationListener);
        }

        Set<EventExecutor> childrenSet = new LinkedHashSet<EventExecutor>(children.length);
        Collections.addAll(childrenSet, children);
        // 创建线程执行器数组只读副本,在迭代查询时使用
        readonlyChildren = Collections.unmodifiableSet(childrenSet);
    }

Netty的NioEventLoop线程被包装成了FastThreadLocalThread线程,同时,NioEventLoop线程的状态由它自身管理,所以每个NioEventLoop线程都要有一个线程执行器。

查看线程执行器:

public final class ThreadPerTaskExecutor implements Executor {
    private final ThreadFactory threadFactory;

    public ThreadPerTaskExecutor(ThreadFactory threadFactory) {
        this.threadFactory = ObjectUtil.checkNotNull(threadFactory, "threadFactory");
    }

    @Override
    public void execute(Runnable command) {
        // 调用线程工厂类的 newThread() 方法包装线程并启动
        threadFactory.newThread(command).start();
    }
}

实际的包装方式:
io.netty.util.concurrent.DefaultThreadFactory

 @Override
    public Thread newThread(Runnable r) {
        // 包装FastThreadLocalRunnable线程,线程名字的前缀为NioEventLoopGroup-
        // 这就也是我们日志中常看到的线程名字的由来,你也可以通过arthas工具查看运行时线程
        Thread t = newThread(FastThreadLocalRunnable.wrap(r), prefix + nextId.incrementAndGet());
        try {
            if (t.isDaemon() != daemon) {
                t.setDaemon(daemon);
            }

            if (t.getPriority() != priority) {
                t.setPriority(priority);
            }
        } catch (Exception ignored) {
            // Doesn't matter even if failed to set.
        }
        return t;
    }

这是我用arthas工具,thread命令截取的一张图,可以看到其线程名字就是以NioEventLoopGroup-开头.
在这里插入图片描述

参考书籍:《Netty源码剖析与应用》

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值