码迷,mamicode.com
首页 > 其他好文 > 详细

selector.select()和selector.selectedKeys()

时间:2017-04-07 00:36:49      阅读:1267      评论:0      收藏:0      [点我收藏+]

标签:ast   不为   selector   his   hyper   eve   ref   ted   blog   

当调用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,这个在第一个链接里也有原因

selector.select()和selector.selectedKeys()

标签:ast   不为   selector   his   hyper   eve   ref   ted   blog   

原文地址:http://www.cnblogs.com/drizzlewithwind/p/6676172.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!