码迷,mamicode.com
首页 > 编程语言 > 详细

Java NIO- Selector 使用示例

时间:2018-03-19 20:49:55      阅读:238      评论:0      收藏:0      [点我收藏+]

标签:sre   post   cto   tor   next   log   ada   reg   continue   

Selector selector = Selector.open();
channel.configureBlocking(false);
SelectionKey key = channel.register(selector, SelectionKey.OP_READ);
while(true) {
  int readyChannels = selector.select();
  if(readyChannels == 0) continue;
  Set selectedKeys = selector.selectedKeys();
  Iterator keyIterator = selectedKeys.iterator();
  while(keyIterator.hasNext()) {
    SelectionKey key = keyIterator.next();
    if(key.isAcceptable()) {
        // a connection was accepted by a ServerSocketChannel.
    } else if (key.isConnectable()) {
        // a connection was established with a remote server.
    } else if (key.isReadable()) {
        // a channel is ready for reading
    } else if (key.isWritable()) {
        // a channel is ready for writing
    }
    keyIterator.remove();
  }
}

 

Java NIO- Selector 使用示例

标签:sre   post   cto   tor   next   log   ada   reg   continue   

原文地址:https://www.cnblogs.com/yongdaimi/p/8604359.html

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