前言
继上一篇讲完了Channel的注册和初始化后,我们看看Netty的Selector是如何创建的,以及Netty是如何对他进行优化的
正文
io.netty.channel.nio.NioEventLoop
// 在NioEventLoop初始化的时候,在构造方法里会调用openSelector()方法
private SelectorTuple openSelector() {
final Selector unwrappedSelector;
try {
unwrappedSelector = provider.openSelector();
} catch (IOException e) {
throw new ChannelException("failed to open a new selector", e);
}
// DISABLE_KEY_SET_OPTIMIZATION 是否禁用优化?,默认是false
if (DISABLE_KEY_SET_OPTIMIZATION) {
// 禁用优化,返回JDK的Selector
return new SelectorTuple(unwrappedSelector);
}
// 初始化JDK的Selector
Object maybeSelectorImplClass = AccessController.doPrivileged(new PrivilegedAction