netty (一) eventLoopGroup

版权声明:转载请注明出处 https://blog.csdn.net/Lee_Suoer/article/details/84929535

MultithreadEventLoopGroup的实现,适用于基于channel的nio selector

EventLoopGroup的子类,负责通过next()方法提供EventExecutor,还负责处理他们的生命周期,允许将他们关闭。

EventExecutor

一个执行器,提供了管理终止的方法和针对一个或多个异步执行任务的future对象来跟踪处理过程。

ExecutorService如果关闭了将会拒绝执行新的tasks.有两种关闭的方式。
(1)shutdown方法会在关闭之前执行已经提交的任务。
(2)shutdownNow方法会将关闭正在处理和将要处理的任务。
关闭之后就没有了新的任务被提交,等待或执行。然后关闭掉,释放资源。
(使用shutdown可以阻止新的任务提交,shutdownNow可以关闭那些延迟时间长的程序)

execute(Runnable)方法用来创建并且返回一个future对象,用来等待执行完成,或者取消执行。
invokeAny和invokeAll通常是用于批量执行有效的方式,等待一个或者全部完成。

无参构造使用了默认的线程数量,ThreadFactory和SelectorProvider

最终是调用了这个方法生成了一个实例 io.netty.util.concurrent.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 {
            // 创建了nThreads个eventExecutor
            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;
                    }
                }
            }
        }
    }

    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) {
       // 为每一个EventExecutor添加一个监听器
        e.terminationFuture().addListener(terminationListener);
    }

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

---------------------------------------------------至此 nioEventLoopGroup初始化完成-------------------------------

猜你喜欢

转载自blog.csdn.net/Lee_Suoer/article/details/84929535