有没有人能解释一下为啥rebuildSelector就能解决空轮询,是每512次selector后加了个rebuild的任务来来防止空的while true吗
来源:4-8 检测IO事件

慕数据5174071
2019-05-25
long time = System.nanoTime();
if (time - TimeUnit.MILLISECONDS.toNanos(timeoutMillis) >= currentTimeNanos) {
selectCnt = 1;
} else if (SELECTOR_AUTO_REBUILD_THRESHOLD > 0 && selectCnt >= SELECTOR_AUTO_REBUILD_THRESHOLD) {
logger.warn("Selector.select() returned prematurely {} times in a row; rebuilding Selector {}.", selectCnt, selector);
this.rebuildSelector(); ### 加了这个为什么就能解决空轮询,是在空轮询512次之后执行一个耗时任务,就像sleep一样吗
selector = this.selector;
selector.selectNow();
selectCnt = 1;
break;
}```
写回答
2回答
-
512 这个数值其实也可以是其他的数字,比如,你也可以设置为500,501 等等,Netty 的做法是,只要连续出现若干次空轮询,那么就把已经产生bug的selector丢掉,重新创建一个。
012019-05-26 -
Kyushu
2021-03-23
老师能不能解读下最新版本的netty关于select这一块的逻辑,感觉简化了很多,rebuild只在select异常和另外一个判断力执行了
00
相似问题