Easy Netty 系列(七):EventLoop

#Netty [字体 ··]

EventLoop

EventLoop 是 Netty 的工作线程,EventLoop 是单线程的,每一个 EventLoop 永远只和一个 Java 线程绑定,这使得 EventLoop 处理读写事件是线程安全的(非共享 Handler)。

Netty 版本:4.1.70.Final

EventLoop 类结构

EventLoop类结构

无论是 EpollEventLoop、NioEventLoop、KQueueEventLoop 都是直接接触 SingleThreadEventLoop 实现的,这个类也再次验证了 EventLoop 是单线程的。

类、接口主要作用

名字类型作用
Executor接口主要定义一个可执行的 execute 方法
ExecutorService接口定义了对任务(Task)的控制方法,如提交(submit)、结束(shutdown)和状态方法(isShutdown)等
ScheduledExecutorService接口定义了定时类任务提交方法,特点是方法入参须传入时间和时间单位
AbstractExecutorService抽象类默认实现了 ExecutorService 的方法
EventExecutorGroup接口定义了组 Executor 的操作方法,核心方法是 next 用于返回一个组管理的 Executor。
EventLoopGroup接口特殊的 EventExecutorGroup,允许注册 Channel,用于选择 Channel
EventExecutor接口定义了一些方法,检测线程是否在 EventLoop 中运行
AbstractEventExecutor抽象类默认 EventExecutor 接口的实现
AbstractScheduledEventExecutor抽象类支持定时任务的 EventExecutor 接口的默认实现
SingleThreadEventExecutor抽象类单线程 EventExecutor 的默认实现,单线程 Executor 基类
EventLoop接口定义获取父 EventLoop 的方法 parent
SingleThreadEventLoop抽象类EventLoop 的抽象基类,它在单个线程中执行所有提交的任务。
NioEventLoop聚合 Nio Selector 对象的类
EpollEventLoop聚合 Epoll fd、Epoll event 的类

EventLoop 核心源码分析

AbstractScheduledEventExecutor

ScheduledEventExecutor 的核心功能是创建执行执行定时任务,这些功能对应的方法是:

1public ScheduledFuture<?> schedule(Runnable command, long delay, TimeUnit unit)
2public <V> ScheduledFuture<V> schedule(Callable<V> callable, long delay, TimeUnit unit)
3public ScheduledFuture<?> scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit)
4public ScheduledFuture<?> scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit)

上边的这些方法最后都会调用一个私有的schedule方法,这个方法是 ScheduledEventExecutor 的核心。

 1    /**
 2     * 核心方法,所有暴露的schedule方法最后都调用此方法
 3     * @param task 可执行的对象(实现Runnable)且具有返回值(实现Future)
 4     * @return task执行结果,可获取执行结果(success/failure),可以监听状态
 5     */
 6    private <V> ScheduledFuture<V> schedule(final ScheduledFutureTask<V> task) {
 7        // 判断在哪里创建(schedule)定时任务
 8        if (inEventLoop()) {
 9            // 在EventLoop线程中
10            // 如果是在EventLoop(handler执行上下文)中创建的定时任务,就放到任务执行队列中
11            scheduleFromEventLoop(task);
12        } else {
13            // 在外部线程 e.g. main线程调用schedule创建定时任务
14
15            // 截止时间 (当前 - ScheduleFutureTask创建时间)
16            final long deadlineNanos = task.deadlineNanos();
17            // 任务提交前回调判断,true 立即执行任务,false 延迟执行
18            if (beforeScheduledTaskSubmitted(deadlineNanos)) {
19                // execute表示调用执行(立即run),是个未实现的方法,未来由子类实现,由父类调用
20                execute(task);
21            } else {
22                // 在不重写父类lazyExecute时默认仍然使用当前EventLoop线程执行
23                lazyExecute(task);
24                // 任务提交之后回调,方法返回true唤醒当前EventLoop线程,false不唤醒
25                if (afterScheduledTaskSubmitted(deadlineNanos)) {
26                    // 官方解释是为了避免线程竞争
27                    // WAKEUP_TASK这个任务run方法没有执行的语句,使线程保持活跃(等待->可执行,持有CPU资源),避免线程竞争CPU开销
28                    execute(WAKEUP_TASK);
29                }
30            }
31        }
32        return task;
33    }
1    // 在EventLoop中创建的定时任务放在taskQueue, 方便EventGroup调度
2	final void scheduleFromEventLoop(final ScheduledFutureTask<?> task) {
3        // nextTaskId a long and so there is no chance it will overflow back to 0
4        scheduledTaskQueue().add(task.setId(++nextTaskId));
5    }

SingleThreadEventExecutor

这个类主要实现了任务的启动、执行、结束

1private void doStartThread();
2// 执行一个任务
3public void execute(Runnable task);
4// 关闭
5public Future<?> shutdownGracefully(long quietPeriod, long timeout, TimeUnit unit);

SingleThreadEventLoop

SingleThreadEventLoop 是 EventLoop 的抽象基类,且继承了 SingleThreadEventExecutor,这个类的主要作用是通过构造器组装父类需要的属性。

NioEventLoop

这个类继承了 SingleThreadEventLoop,内部聚合 Nio Selector,作用是将 Channel 注册到 Selector 并且在事件循环中对这些进行多路复用。

1    /**
2     * The NIO {@link Selector}.
3     */
4    private Selector selector;
5    private Selector unwrappedSelector;
6    private SelectedSelectionKeySet selectedKeys;

EOF


博客没有评论系统,可以通过 邮件 评论和交流。 Top↑