【问题标题】:Some doubts about Netty NioEventLoop关于 Netty NioEventLoop 的一些疑惑
【发布时间】:2022-01-21 19:09:39
【问题描述】:

Netty 版本 4.1.1.7 最终版。我想知道 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;
                }

           // ...
      }

}

【问题讨论】:

    标签: java netty


    【解决方案1】:

    如果到达此行,则已决定选择操作不应等待某些 Channel / SelectionKey 准备好或等待超时。

    但对于selector 上的进一步操作,了解哪些Channels 已准备好处理并因此更新SelectionKeys 仍然很重要。

    这就是Selector.selectNow() 所做的:

    选择一组键,其对应通道已准备好进行 I/O 操作。

    此方法执行非阻塞选择操作。如果自上次选择操作以来没有通道成为可选通道,则此方法立即返回零。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-01-29
      • 2012-08-20
      • 1970-01-01
      • 2016-02-08
      • 2019-10-03
      • 1970-01-01
      • 2011-11-27
      • 1970-01-01
      相关资源
      最近更新 更多