...
//通过key存储的集合
private final Map<SelectionKey, Runnable> inputCallbackMap = new HashMap<>();
...
//塞入集合
if (key == null) {
try {
//注册selector得到key
key = channel.register(selector, registerOps);
//根据key
outputCallbackMap.put(key, callback);
} catch (ClosedChannelException ignore) {
return null;
}
}
...
//调用handle,根据SelectionKey获取runnable进行回调
try {
if (readSelector.select() == 0) {
waitSelection(inRegInput);
continue;
}
Set<SelectionKey> selectionKeys = readSelector.selectedKeys();
for (SelectionKey selectionKey : selectionKeys) {
if (selectionKey.isValid()) {
handleSelection(selectionKey, SelectionKey.OP_READ, inputCallbackMap, inputHandlePool);
}
}
selectionKeys.clear();//这里clear,把selectionKey都置空的是咋回事???
} catch (IOException e) {
e.printStackTrace();
}
这边clear掉以后,再unRegister的时候,能remove掉对应的runnable么?
理论+实践,系统且深入掌握Socket核心技术,从容应对各种Socket应用场景的不二之选
了解课程