简体   繁体   English

关于 Netty NioEventLoop 的一些疑惑

[英]Some doubts about Netty NioEventLoop

Netty version 4.1.1.7 Final. Netty 版本 4.1.1.7 最终版。 I wonder what the if is for and why you must select#selectNow before working on the task.我想知道 if 的用途是什么以及为什么在处理任务之前必须选择#selectNow。

// io.netty.channel.nio.NioEventLoop#select

      private void select(boolean oldWakenUp) throws IOException {
             // ...
             for (;;) {
          long timeoutMillis = (selectDeadLineNanos - currentTimeNanos + 500000L) / 1000000L;
                if (timeoutMillis <= 0) {
                    if (selectCnt == 0) {
                        selector.selectNow();
                        selectCnt = 1;
                    }
                    break;
                }

           // ...
      }

}

If this line is reached it is already decided that the select operation should not wait for some Channel / SelectionKey to become ready or wait for a timeout.如果到达此行,则已经确定 select 操作不应等待某些Channel / SelectionKey准备好或等待超时。

But for the further operations on the selector it is still important to know which Channel s are ready to be processed and therefore update the SelectionKey s.但是对于selector上的进一步操作,知道哪些Channel准备好处理并因此更新SelectionKey仍然很重要。

And this is what Selector.selectNow() does:这就是Selector.selectNow()所做的:

Selects a set of keys whose corresponding channels are ready for I/O operations.选择一组键,其对应的通道已准备好进行 I/O 操作。

This method performs a non-blocking selection operation.该方法执行非阻塞选择操作。 If no channels have become selectable since the previous selection operation then this method immediately returns zero.如果自上一次选择操作以来没有通道变得可选择,则此方法立即返回零。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM