NioEventLoop 继承图:
说明:
- ScheduleExecutorService 接口表示是一个定时任务接口,EventLoop 可以接受定时任务
- EventLoop 接口: 一旦 Channel 注册了,就处理该 Channel 对应的所有 I/O 操作
- SingleThreadEventExecutor 表示这是一个的那个线程的线程池
- EvenetLoop 是一个单例的线程池,里面包含有一个死循环的线程不断的做着 3件事情: 监听端口、处理端口事件、处理队列事件。 每个 EventLoop 都可以绑定多个 Channel, 而每个 Channel 始终只能由一个 EventLoop 来处理
SingleThreadEventExecutor execute:
private void execute(Runnable task, boolean immediate) {
// 判断该 EventLoop 的线程是否是当前线程,
boolean inEventLoop = inEventLoop();
addTask(task);
// 如果是, 直接添加到任务队列中去,如果不是,则尝试启动线程(但由于线程是单个的,因此只能启动一次),随后再将任务添加到队列中去
if (!inEventLoop) {
startThread();
if (isShutdown()) {
boolean reject = false;
try {
if (removeTask(task)) {
reject = true;
}
} catch (UnsupportedOperationException e) {
// 如果线程已经停止,并且删除任务失败,则执行拒绝策略,默认是抛出异常
if (reject) {
reject();
}
}
}
// 如果 addTaskWakersUp 是 false, 并且任务不是 NonWakeupRunable 类型的,就尝试唤醒 selector。 这个时候,阻塞的selector 的线程就会立即返回
if (!addTaskWakesUp && immediate) {
wakeup(inEventLoop);
}
}
addTask 和 offerTask:
protected void addTask(Runnable task) {
ObjectUtil.checkNotNull(task, "task");
// 提供任务
if (!offerTask(task)) {
// 失败拒绝
reject(task);
}
}
final boolean offerTask(Runnable task) {
// 如果 shutdown 则拒绝
if (isShutdown()) {
reject();
}
return taskQueue.offer(task);
}
startThread:
private void startThread() {
// 首先判断是否启动过了,保证 EventLoop 只有一个线程
if (state == ST_NOT_STARTED) {
// 如果没有启动过,则尝试使用CAS 将 state 状态改为 ST_STARTED
if (STATE_UPDATER.compareAndSet(this, ST_NOT_STARTED, ST_STARTED)) {
boolean success = false;
try {
doStartThread();
success = true;
} finally {
if (!success) {
STATE_UPDATER.compareAndSet(this, ST_STARTED, ST_NOT_STARTED);
}
}
}
}
}
doStartThread:
private void doStartThread() {
assert thread == null;
// 首先调用 executor 的 execute 方法,这个 executor 就是在创建 EventLoopGroup 的时候创建的 ThreadPerTaskExecutor 类。该 execute 方法会将 Runable 包装成 Netty 的 FastThreadLocalThread
executor.execute(new Runnable() {
@Override
public void run() {
thread = Thread.currentThread();
// 判断线程中中断状态
if (interrupted) {
thread.interrupt();
}
boolean success = false;
// 设置最后一次的执行时间
updateLastExecutionTime();
try {
// 执行当前 NioEventLoop 的 run 方法,这个方法是个死循环,是整个 EventLoop 的核心
SingleThreadEventExecutor.this.run();
success = true;
} catch (Throwable t) {
logger.warn("Unexpected exception from an event executor: ", t);
} finally {
for (;;) {
int oldState = state;
// 使用 CAS 不断修改 state 状态,当线程 Loop 结束的时候,关闭线程
if (oldState >= ST_SHUTTING_DOWN || STATE_UPDATER.compareAndSet(
SingleThreadEventExecutor.this, oldState, ST_SHUTTING_DOWN)) {
break;
}
}
// Check if confirmShutdown() was called at the end of the loop.
if (success && gracefulShutdownStartTime == 0) {
if (logger.isErrorEnabled()) {
logger.error("Buggy " + EventExecutor.class.getSimpleName() + " implementation; " +
SingleThreadEventExecutor.class.getSimpleName() + ".confirmShutdown() must " +
"be called before run() implementation terminates.");
}
}
try {
// Run all remaining tasks and shutdown hooks. At this point the event loop
// is in ST_SHUTTING_DOWN state still accepting tasks which is needed for
// graceful shutdown with quietPeriod.
for (;;) {
if (confirmShutdown()) {
break;
}
}
// Now we want to make sure no more tasks can be added from this point. This is
// achieved by switching the state. Any new tasks beyond this point will be rejected.
for (;;) {
int oldState = state;
if (oldState >= ST_SHUTDOWN || STATE_UPDATER.compareAndSet(
SingleThreadEventExecutor.this, oldState, ST_SHUTDOWN)) {
break;
}
}
// 确认是否关闭
confirmShutdown();
} finally {
try {
// 执行 cleanup 操作
cleanup();
} finally {
// Lets remove all FastThreadLocals for the Thread as we are about to terminate and notify
// the future. The user may block on the future and once it unblocks the JVM may terminate
// and start unloading classes.
// See https://github.com/netty/netty/issues/6596.
FastThreadLocal.removeAll();
// 更新状态
STATE_UPDATER.set(SingleThreadEventExecutor.this, ST_TERMINATED);
// 释放当前线程锁
threadLock.countDown();
int numUserTasks = drainTasks();
// 如果任务队列不是空,则打印队列中还有多少个未完成的任务
if (numUserTasks > 0 && logger.isWarnEnabled()) {
logger.warn("An event executor terminated with " +
"non-empty task queue (" + numUserTasks + ')');
}
// 回调 terminationFuture 方法
terminationFuture.setSuccess(null);
}
}
}
}
});
}
run 方法:
// NioEventLoop
protected void run() {
int selectCnt = 0;
for (;;) {
try {
int strategy;
try {
strategy = selectStrategy.calculateStrategy(selectNowSupplier, hasTasks());
switch (strategy) {
case SelectStrategy.CONTINUE:
continue;
case SelectStrategy.BUSY_WAIT:
// fall-through to SELECT since the busy-wait is not supported with NIO
case SelectStrategy.SELECT:
long curDeadlineNanos = nextScheduledTaskDeadlineNanos();
if (curDeadlineNanos == -1L) {
curDeadlineNanos = NONE; // nothing on the calendar
}
nextWakeupNanos.set(curDeadlineNanos);
try {
if (!hasTasks()) {
strategy = select(curDeadlineNanos);
}
} finally {
// This update is just to help block unnecessary selector wakeups
// so use of lazySet is ok (no race condition)
nextWakeupNanos.lazySet(AWAKE);
}
// fall through
default:
}
} catch (IOException e) {
// If we receive an IOException here its because the Selector is messed up. Let's rebuild
// the selector and retry. https://github.com/netty/netty/issues/8566
rebuildSelector0();
selectCnt = 0;
handleLoopException(e);
continue;
}
selectCnt++;
cancelledKeys = 0;
needsToSelectAgain = false;
final int ioRatio = this.ioRatio;
boolean ranTasks;
if (ioRatio == 100) {
try {
if (strategy > 0) {
processSelectedKeys();
}
} finally {
// Ensure we always run tasks.
ranTasks = runAllTasks();
}
} else if (strategy > 0) {
final long ioStartTime = System.nanoTime();
try {
processSelectedKeys();
} finally {
// Ensure we always run tasks.
final long ioTime = System.nanoTime() - ioStartTime;
ranTasks = runAllTasks(ioTime * (100 - ioRatio) / ioRatio);
}
} else {
ranTasks = runAllTasks(0); // This will run the minimum number of tasks
}
if (ranTasks || strategy > 0) {
if (selectCnt > MIN_PREMATURE_SELECTOR_RETURNS && logger.isDebugEnabled()) {
logger.debug("Selector.select() returned prematurely {} times in a row for Selector {}.",
selectCnt - 1, selector);
}
selectCnt = 0;
} else if (unexpectedSelectorWakeup(selectCnt)) { // Unexpected wakeup (unusual case)
selectCnt = 0;
}
} catch (CancelledKeyException e) {
// Harmless exception - log anyway
if (logger.isDebugEnabled()) {
logger.debug(CancelledKeyException.class.getSimpleName() + " raised by a Selector {} - JDK bug?",
selector, e);
}
} catch (Error e) {
throw e;
} catch (Throwable t) {
handleLoopException(t);
} finally {
// Always handle shutdown even if the loop processing threw an exception.
try {
if (isShuttingDown()) {
closeAll();
if (confirmShutdown()) {
return;
}
}
} catch (Error e) {
throw e;
} catch (Throwable t) {
handleLoopException(t);
}
}
}
}
小结:
每次执行 execute 方法都是向队列中添加任务。当第一次添加时就启动线程,执行 run 方法,而 run 方法是整个 EventLoop 的核心,就像 EventLoop 的名字一样,不停的 Loop
- 调用 selector 的 select 方法,默认阻塞一秒钟,如果有定时任务,则在定时任务剩余时间基础上加上0.5 秒进行阻塞。当执行 execute 方法的时候,也就是添加任务的时候,会唤醒 selector,防止 selector 阻塞时间过长
- 当 selector 方式的时候,会调用 processSelectedKeys 方法对 selectKey 进行处理
- 当 processSelectedKeys 方法执行结束后,则按照 ioRatio 的比例执行 runAllTasks 方法,默认是 IO 任务时间和非 IO任务时间是相同的,可根据应用特点进行调优。 比如 非 IO 任务比较多时,将ioRatio 调小一点,这样非 IO 任务就能执行的长一点。防止队列中积攒过多的任务。