【Netty源码学习】EventLoopGroup

来源:互联网 发布:淘宝上卖工艺品好卖吗 编辑:程序博客网 时间:2024/06/15 15:19

在上一篇博客【Netty源码解析】入门示例中我们介绍了一个Netty入门的示例代码,接下来的博客我们会分析一下整个demo工程运行过程的运行机制。 无论在Netty应用的客户端还是服务端都首先会初始化EventLoopGroup,其封装了线程的创建,以及loop和线程的关联等内容。

EventLoopGroup group = new NioEventLoopGroup();  
接下来我们一步一步看NioEventLoopGroup在初始化过程中都做了什么事情,其源码如下:

/** * {@link MultithreadEventLoopGroup} implementations which is used for NIO {@link Selector} based {@link Channel}s. */public class NioEventLoopGroup extends MultithreadEventLoopGroup {    /**     * Create a new instance using the default number of threads, the default {@link ThreadFactory} and     * the {@link SelectorProvider} which is returned by {@link SelectorProvider#provider()}.     */    public NioEventLoopGroup() {        this(0);    }    /**     * Create a new instance using the specified number of threads, {@link ThreadFactory} and the     * {@link SelectorProvider} which is returned by {@link SelectorProvider#provider()}.     */    public NioEventLoopGroup(int nThreads) {        this(nThreads, (Executor) null);    }    /**     * Create a new instance using the specified number of threads, the given {@link ThreadFactory} and the     * {@link SelectorProvider} which is returned by {@link SelectorProvider#provider()}.     */    public NioEventLoopGroup(int nThreads, ThreadFactory threadFactory) {        this(nThreads, threadFactory, SelectorProvider.provider());    }    public NioEventLoopGroup(int nThreads, Executor executor) {        this(nThreads, executor, SelectorProvider.provider());    }    /**     * Create a new instance using the specified number of threads, the given {@link ThreadFactory} and the given     * {@link SelectorProvider}.     */    public NioEventLoopGroup(            int nThreads, ThreadFactory threadFactory, final SelectorProvider selectorProvider) {        this(nThreads, threadFactory, selectorProvider, DefaultSelectStrategyFactory.INSTANCE);    }    public NioEventLoopGroup(int nThreads, ThreadFactory threadFactory,        final SelectorProvider selectorProvider, final SelectStrategyFactory selectStrategyFactory) {        super(nThreads, threadFactory, selectorProvider, selectStrategyFactory, RejectedExecutionHandlers.reject());    }    public NioEventLoopGroup(            int nThreads, Executor executor, final SelectorProvider selectorProvider) {        this(nThreads, executor, selectorProvider, DefaultSelectStrategyFactory.INSTANCE);    }    public NioEventLoopGroup(int nThreads, Executor executor, final SelectorProvider selectorProvider,                             final SelectStrategyFactory selectStrategyFactory) {        super(nThreads, executor, selectorProvider, selectStrategyFactory, RejectedExecutionHandlers.reject());    }    public NioEventLoopGroup(int nThreads, Executor executor, EventExecutorChooserFactory chooserFactory,                             final SelectorProvider selectorProvider,                             final SelectStrategyFactory selectStrategyFactory) {        super(nThreads, executor, chooserFactory, selectorProvider, selectStrategyFactory,                RejectedExecutionHandlers.reject());    }    public NioEventLoopGroup(int nThreads, Executor executor, EventExecutorChooserFactory chooserFactory,                             final SelectorProvider selectorProvider,                             final SelectStrategyFactory selectStrategyFactory,                             final RejectedExecutionHandler rejectedExecutionHandler) {        super(nThreads, executor, chooserFactory, selectorProvider, selectStrategyFactory, rejectedExecutionHandler);    }    /**     * Sets the percentage of the desired amount of time spent for I/O in the child event loops.  The default value is     * {@code 50}, which means the event loop will try to spend the same amount of time for I/O as for non-I/O tasks.     */    public void setIoRatio(int ioRatio) {        for (EventExecutor e: this) {            ((NioEventLoop) e).setIoRatio(ioRatio);        }    }    /**     * Replaces the current {@link Selector}s of the child event loops with newly created {@link Selector}s to work     * around the  infamous epoll 100% CPU bug.     */    public void rebuildSelectors() {        for (EventExecutor e: this) {            ((NioEventLoop) e).rebuildSelector();        }    }    @Override    protected EventLoop newChild(Executor executor, Object... args) throws Exception {        return new NioEventLoop(this, executor, (SelectorProvider) args[0],            ((SelectStrategyFactory) args[1]).newSelectStrategy(), (RejectedExecutionHandler) args[2]);    }}
由上面的源码我们了解到NioEventLoopGroup构造函数初始化的结果是调用父类MultithreadEventLoopGroup的构造函数,接下来我们看一下MultithreadEventLoopGroup的构造函数做了什么事情。

public abstract class MultithreadEventExecutorGroup extends AbstractEventExecutorGroup {    private final EventExecutor[] children;    private final Set<EventExecutor> readonlyChildren;    private final AtomicInteger terminatedChildren = new AtomicInteger();    private final Promise<?> terminationFuture = new DefaultPromise(GlobalEventExecutor.INSTANCE);    private final EventExecutorChooserFactory.EventExecutorChooser chooser;       protected MultithreadEventExecutorGroup(int nThreads, ThreadFactory threadFactory, Object... args) {        this(nThreads, threadFactory == null ? null : new ThreadPerTaskExecutor(threadFactory), args);    }    protected MultithreadEventExecutorGroup(int nThreads, Executor executor, Object... args) {        this(nThreads, executor, DefaultEventExecutorChooserFactory.INSTANCE, args);    }    /**     * 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());        }        children = new EventExecutor[nThreads];        for (int i = 0; i < nThreads; i ++) {            boolean success = false;            try {                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) {            e.terminationFuture().addListener(terminationListener);        }        Set<EventExecutor> childrenSet = new LinkedHashSet<EventExecutor>(children.length);        Collections.addAll(childrenSet, children);        readonlyChildren = Collections.unmodifiableSet(childrenSet);    }    protected ThreadFactory newDefaultThreadFactory() {        return new DefaultThreadFactory(getClass());    }    @Override    public EventExecutor next() {        return chooser.next();    }    @Override    public Iterator<EventExecutor> iterator() {        return readonlyChildren.iterator();    }    /**     * Return the number of {@link EventExecutor} this implementation uses. This number is the maps     * 1:1 to the threads it use.     */    public final int executorCount() {        return children.length;    }    /**     * Create a new EventExecutor which will later then accessible via the {@link #next()}  method. This method will be     * called for each thread that will serve this {@link MultithreadEventExecutorGroup}.     *     */    protected abstract EventExecutor newChild(Executor executor, Object... args) throws Exception;    @Override    public Future<?> shutdownGracefully(long quietPeriod, long timeout, TimeUnit unit) {        for (EventExecutor l: children) {            l.shutdownGracefully(quietPeriod, timeout, unit);        }        return terminationFuture();    }    @Override    public Future<?> terminationFuture() {        return terminationFuture;    }    @Override    @Deprecated    public void shutdown() {        for (EventExecutor l: children) {            l.shutdown();        }    }    @Override    public boolean isShuttingDown() {        for (EventExecutor l: children) {            if (!l.isShuttingDown()) {                return false;            }        }        return true;    }    @Override    public boolean isShutdown() {        for (EventExecutor l: children) {            if (!l.isShutdown()) {                return false;            }        }        return true;    }    @Override    public boolean isTerminated() {        for (EventExecutor l: children) {            if (!l.isTerminated()) {                return false;            }        }        return true;    }    @Override    public boolean awaitTermination(long timeout, TimeUnit unit)            throws InterruptedException {        long deadline = System.nanoTime() + unit.toNanos(timeout);        loop: for (EventExecutor l: children) {            for (;;) {                long timeLeft = deadline - System.nanoTime();                if (timeLeft <= 0) {                    break loop;                }                if (l.awaitTermination(timeLeft, TimeUnit.NANOSECONDS)) {                    break;                }            }        }        return isTerminated();    }}
从上面代码的构造函数中我们发现了真正处理功能

(1)executor = new ThreadPerTaskExecutor(newDefaultThreadFactory()); 首先创建一个单线程执行的线程池

(2)children = new EventExecutor[nThreads];  创建处理器两倍数目的处理线程数组

(3)children[i] = newChild(executor, args)  实例化线程数组,具体实现在NioEventLoopGroup

@Override    protected EventLoop newChild(Executor executor, Object... args) throws Exception {        return new NioEventLoop(this, executor, (SelectorProvider) args[0],            ((SelectStrategyFactory) args[1]).newSelectStrategy(), (RejectedExecutionHandler) args[2]);    }

这样EventLoopGroup的主要功能就完成了,其主要机制就是创建了一定数目的NioEventLoop。


EventLoopGroup类图结构:



        





1 0
原创粉丝点击