Netty-EventLoop

1. public interface EventLoop extends EventExecutor, EventLoopGroup

2. public interface EventExecutor extends EventExecutorGroup

3. public interface EventExecutorGroup extends ScheduledExecutorService, Iterable<EventExecutor>

4. public interface ScheduledExecutorService extends ExecutorService

5. public interface ExecutorService extends Executor

public abstract class SingleThreadEventLoop extends SingleThreadEventExecutor implements EventLoop

EventLoop 其实就是 Executor,也就是一个线程池。对于执行器来说,它可以有多个线程,也可以有一个线程。

ExecutorService 通过对外暴露接口,将要执行的任务放在任务队列中,执行器中的线程会从队列中拿走任务去执行,

将结果返回。

public final class NioEventLoop extends SingleThreadEventLoop

@Override
 protected EventExecutor newChild(
            ThreadFactory threadFactory, Object... args) throws Exception {
        return new NioEventLoop(this, threadFactory, (SelectorProvider) args[0]);
 }

  Netty 中使用了只拥有一个的线程的线程池 EventExecutor, 也就是在 new NioEventLoopGroup( count ) 时,其实创建的 count 个 线程池,这些线程池

里面也只有一个线程。结果就是创建了 count 个线程,然后就就是 将线程池和Channel绑定一起来,这样的结果就是 channel 的操作永远是在相同的一个线程

里被执行。

posted @ 2018-09-07 10:18  iDragon  阅读(146)  评论(0编辑  收藏  举报