构造函数
NioEventLoop(NioEventLoopGroup parent, Executor executor, SelectorProvider selectorProvider,
SelectStrategy strategy, RejectedExecutionHandler rejectedExecutionHandler) {
super(parent, executor, false, DEFAULT_MAX_PENDING_TASKS, rejectedExecutionHandler);
if (selectorProvider == null) {
throw new NullPointerException("selectorProvider");
}
if (strategy == null) {
throw new NullPointerException("selectStrategy");
}
provider = selectorProvider;
final SelectorTuple selectorTuple = openSelector(); //SelectorProvider.provider().openSelector()
selector = selectorTuple.selector; // SelectedSelectionKeySetSelector 包装unwrappedSelector
unwrappedSelector = selectorTuple.unwrappedSelector; //原始的nio创建对象 比如WindowsSelectorImpl
selectStrategy = strategy; // DefaultSelectStrategy
}
openSelector 优化
private SelectorTuple openSelector() {
final Selector unwrappedSelector;
try {
unwrappedSelector = provider.openSelector(); //创建java原生selector
} catch (IOException e) {
throw new ChannelException("failed to open a new selector", e);
}
//是否需要优化,默认需要DISABLE_KEYSET_OPTIMIZATION=false
if (DISABLE_KEY_SET_OPTIMIZATION) {
return new SelectorTuple(unwrappedSelector);
}
//尝试获取sun.nio.ch.SelectorImpl的class对象
Object maybeSelectorImplClass = AccessController.doPrivileged(new PrivilegedAction
WindowsSelectorImpl
final class WindowsSelectorImpl extends SelectorImpl {}
public abstract class SelectorImpl extends AbstractSelector {
protected Set selectedKeys = new HashSet(); //selectedKeys
....................
protected SelectorImpl(SelectorProvider var1) {
super(var1);
if (Util.atBugLevel("1.4")) {
this.publicKeys = this.keys;
this.publicSelectedKeys = this.selectedKeys; // 其实就是selectedKeys
} else {
this.publicKeys = Collections.unmodifiableSet(this.keys);
this.publicSelectedKeys = Util.ungrowableSet(this.selectedKeys); 其实就是selectedKeys
}
}
SelectedSelectionKeySet
final class SelectedSelectionKeySet extends AbstractSet {
SelectionKey[] keys;
int size;
SelectedSelectionKeySet() {
keys = new SelectionKey[1024];
}
}
SelectorTuple
private static final class SelectorTuple {
final Selector unwrappedSelector;
final Selector selector;
SelectorTuple(Selector unwrappedSelector) {
this.unwrappedSelector = unwrappedSelector;
this.selector = unwrappedSelector;
}
SelectorTuple(Selector unwrappedSelector, Selector selector) {
this.unwrappedSelector = unwrappedSelector;
this.selector = selector; // SelectedSelectionKeySetSelector 这里的delegate=unwrappedSelector
}
}
//是否关闭优化,默认需要DISABLE_KEYSET_OPTIMIZATION=false
if (DISABLE_KEY_SET_OPTIMIZATION) {
return new SelectorTuple(unwrappedSelector);
}
SelectedSelectionKeySetSelector
final class SelectedSelectionKeySetSelector extends Selector {
private final SelectedSelectionKeySet selectionKeys;
private final Selector delegate;
SelectedSelectionKeySetSelector(Selector delegate, SelectedSelectionKeySet selectionKeys) {
this.delegate = delegate; // jdk原生Selector的实现类
this.selectionKeys = selectionKeys; //数组实现的SelectedSelectionKeySet
}
}
在windows操作系统下,openSelector,在优化情况下,本质就是把Selector的实现类WindowsSelectorImpl中的selectedKeys、publicSelectedKeys替换为新的Netty自己实现的SelectedSelectionKeySet(数组实现) 以前的selectedKeys和publicSelectedKeys是hashset实现的,而hashset其实就是1个hashmap 返回的SelectorTuple, 关闭优化情况下,Selector unwrappedSelector 和 Selector selector 是同一个,都是jdk的原生Selector的实现。
未关闭优化情况下,调用 SelectorTuple(Selector unwrappedSelector, Selector selector) 构造函数,则selector是SelectedSelectionKeySetSelector类,内部delegate是原生的Selector实现类,selectionKeys是Netty自己数组实现的SelectedSelectionKeySet