6

nio做客户端是否是bug????在线等

 8 months ago
source link: https://www.jdon.com/18682.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

nio做客户端是否是bug????在线等

客户端事件处理线程代码如下:

while (!bStop) {
try {
int n = selector.select();

// 如果要shutdown,关闭selector退出
if (bStop) {
selector.close();
break;
}

// 如果select返回大于0,处理事件
if (n > 0) {
for (Iterator i = selector.selectedKeys().iterator(); i.hasNext(); ) {
// 得到下一个Key
SelectionKey sk = (SelectionKey) i.next();
i.remove();

// 处理
if (sk.isConnectable()) {
this.event.processConnect(sk);
}
else if (sk.isReadable()) {
this.event.processRead(sk);
sk.cancel();
}
else if (sk.isWritable()) {
this.event.processWrite(sk);
sk.cancel();
}
}
}
else {
//?????注册write后,不停的执行
}
}
catch (IOException e) {
log.error(e.getMessage());
this.event.processError(e);
}
catch (Exception e) {
log.error("数据处理错误: " + e.getMessage());
}
}
log.debug("nio监听线程已经退出");
}
代码流程如上,当我注册write时,selector不停的返回0,这是否是个bug,我觉得如果有在连接或当selector返回值为0时注册一个write,就只有当我write时,事件才会触发,这是不是nio的一个bug

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK