selector.select()和selector.selectedKeys()

当调用selector.select()时会阻塞:

This method performs a blocking selection operation. It returns only after at least one channel is 
 selected, this selector's wakeup method is invoked, or the current thread is interrupted, whichever 
 comes first. 

其返回值有可能为0:

The number of keys, possibly zero,
 whose ready-operation sets were updated

另请见,解释了select后返回值为0,但selector.selectedKeys()不为空:Java NIO Selector select() returns 0 although channels are ready

 

 

selector.selectedKeys()返回的Set<SelectionKey>需要删除SelectionKey,原因请见:Why the key should be removed in `selector.selectedKeys().iterator()` in java nio?

另外,Set<SelectionKey>删除SelectionKey需要使用iterator,这个在第一个链接里也有原因

转载于:https://www.cnblogs.com/drizzlewithwind/p/6676172.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值