本文介绍了Netty体系结构-有关NioWorker循环的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究NioWorker.run()方法并试图了解其工作方式.下面是该代码的简化版本:

I am looking into the NioWorker.run() method and trying to understand how it works.Below is the simplified version of the code:

    for(;;) {
      try {
        SelectorUtil.select(selector);

        if (wakenUp.get()) {
          selector.wakeup();
        }

        cancelledKeys = 0;
        processRegisterTaskQueue();
        processWriteTaskQueue();
        processSelectedKeys(selector.selectedKeys());

      } catch (Throwable t) {
      }
    }

更不用说了,但是我有一些疑问:

More-less it is clear what it does however I have some questions:

1..Selector.select(选择器)以500毫秒的超时时间执行选择.为什么它不只是阻塞的呼叫?

1. Selector.select(selector) performs selection with 500 millisecond timeout.Why it is not just a blocking call?

2..以下片段的目的是什么?为什么我们需要执行唤醒?

2. What is the purpose of the below fragment? Why do we need to perform wakeup?

    if (wakenUp.get()) {
       selector.wakeup();
    }

预先感谢

推荐答案

  1. 我们使用基于时间的阻塞Selector.select(..)调用,因为我们还通过processRegisterTaskQueue()方法来处理向工作线程的新通道注册.如果我们不使用基于时间的通话,我们将有放慢速度的风险.我们甚至将Selector.select(..)调用更改为在最新版本中使用10ms.

  1. We use a time-based blocking Selector.select(..) call because we also handle the registering of new channels to the worker via the processRegisterTaskQueue() method. If we would not use a time-based call we would get the risk to slow down things. We even changed the Selector.select(..) call to use 10ms in recent versions.

请参阅[1]上的评论

[1] https://github.com/netty/netty/blob/3/src/main/java/org/jboss/netty/channel/socket/nio/AbstractNioWorker.java#L202

这篇关于Netty体系结构-有关NioWorker循环的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-16 20:54
查看更多