*对于nio的非阻塞I/O操作,使用Selector获取哪些I/O准备就绪,注册的SelectionKey集合记录关联的Channel这些信息.SelectionKey记录Channel对buffer的操作方式.

---SelectableChannel,Selector,SelectionKey是nio中Channel操作的3个主要部件.

---对应关系,

一个SelectableChannel,记录了一组注册的SelectionKey[]

一个SelectionKey,关联了一个Channel和一个Selector.

一个Selector,维护着注册的一组SelectionKey

*使用示例

---注,这段代码来自"TCP/IP Sockets in Java",典型示例,我做了注解

package chapter5;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.util.Iterator; public class TCPServerSelector {
private static final int BUFSIZE = 256; //Buffer,EchoSelectorProtocol
private static final int TIMEOUT = 3000; //Selector.select(long timeout) public static void main(String[] args) throws IOException {
if(args.length<1){
throw new IllegalArgumentException("Parameter(s): <Port> ...");
} Selector selector =Selector.open(); //创建Selector实例
for(String arg:args){
ServerSocketChannel listnChannel =ServerSocketChannel.open();
listnChannel.socket().bind(new InetSocketAddress(Integer.parseInt(arg)));
listnChannel.configureBlocking(false); //nonblocking
listnChannel.register(selector, SelectionKey.OP_ACCEPT); //Channel注册selector,并告知channel感兴趣的操作
} TCPProtocol protocol =new EchoSelectorProtocol(BUFSIZE);
while(true){ //循环
if(selector.select(TIMEOUT)==0){ //返回准备就绪I/O的SelectionKey数
System.out.println("."); //to do others
continue;
} Iterator<SelectionKey> keyIter =selector.selectedKeys().iterator();//获取已选的SelectionKey集合
while(keyIter.hasNext()){ //遍历key,根据key的类型做相应处理
SelectionKey key =keyIter.next();
if(key.isAcceptable())
protocol.handleAccept(key); if(key.isReadable())
protocol.handleRead(key); //SelectionKey is invalid if it is cancelled, its channel is closed, or its selector is closed.
if(key.isValid() && key.isWritable())
protocol.handleWrite(key); keyIter.remove(); //手动清空,因为Selector只会在已选的SelectionKey集中添加
}
} }
}
package chapter5;

import java.io.IOException;
import java.nio.channels.SelectionKey; public interface TCPProtocol {
void handleAccept(SelectionKey key) throws IOException;
void handleRead(SelectionKey key) throws IOException;
void handleWrite(SelectionKey key) throws IOException;
} package chapter5; import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel; public class EchoSelectorProtocol implements TCPProtocol { private int bufSize; public EchoSelectorProtocol(int bufSize){
this.bufSize =bufSize;
} @Override
public void handleAccept(SelectionKey key) throws IOException {
SocketChannel clntChan = ((ServerSocketChannel)key.channel()).accept();
clntChan.configureBlocking(false);
//对于已注册Selector的Channel,再次调用就更新注册信息,这里更新了SelectionKey的类型和附件,附件是需要操作的buffer.
clntChan.register(key.selector(), SelectionKey.OP_READ,ByteBuffer.allocate(bufSize));
} @Override
public void handleRead(SelectionKey key) throws IOException {
SocketChannel clntChan =(SocketChannel)key.channel();
ByteBuffer buf =(ByteBuffer) key.attachment(); //获取附件
long bytesRead = clntChan.read(buf);
if(bytesRead==-1) //channel已读到结束位置
clntChan.close();
else if(bytesRead > 0)
key.interestOps(SelectionKey.OP_READ|SelectionKey.OP_WRITE);
} @Override
public void handleWrite(SelectionKey key) throws IOException {
ByteBuffer buf =(ByteBuffer) key.attachment();
buf.flip();
SocketChannel clntChan =(SocketChannel) key.channel();
clntChan.write(buf);
if(!buf.hasRemaining()){
key.interestOps(SelectionKey.OP_READ); //设置Key的兴趣集
}
buf.compact();
} }

看了代码,如何使用就清楚了吧:).

下面再做些细节说明

*Selector

---更新准备好的SelectionKey,移除isValid()为false的SelectionKey

select() //阻塞等待,直至一个channel准备好或调用wakeup()才返回

select(long timeout) //如上,返回条件多了个超时时间

selectNow() //非阻塞,会立刻返回,没有时返回值=0

wakeup() //使得Selector返回

注意,select()会在上次已选择的键集中添加这次的可用键,故在2次select之间,手动移除已处理的SelectionKey.

*SelectionKey

---兴趣操作集,通过它就可以知道channel可以去做哪些事了.有4种类型,如下:

public static final int OP_READ = 1 << 0;

public static final int OP_ACCEPT = 1 << 4;

public static final int OP_WRITE = 1 << 2;

public static final int OP_CONNECT = 1 << 3;

通过SelectionKey.interestOps(int ops)就可以配置这些值

---附件,主要作用是为channel处理提供辅助信息,如上面示例中att为ByteBuffer

SelectionKey.attach(Object ob) //添加附件,另一种方式SelectableChannel.register(Selector sel, int ops, Object att)

SelectionKey.attachment()  //获取附件

---SelectionKey.cancel(),永久的注销键,加入Selector的注销集中,在下次select()时被移除

NIO组件Selector调用实例的更多相关文章

  1. NIO组件Selector工作机制详解(上)

    转自:http://blog.csdn.net/haoel/article/details/2224055 一.  前言 自从J2SE 1.4版本以来,JDK发布了全新的I/O类库,简称NIO,其不但 ...

  2. NIO组件 Selector(选择器)

    简介 使用Selector(选择器), 可以使用一个线程处理多个客户端连接. Selector 能够检测多个注册的通道上是否有事件发生(多个Channel以事件的方式可以注册到同一个Selector) ...

  3. NIO组件Selector详解

    Selector(选择器)是Java NIO中能够检测一到多个NIO通道,并能够知晓通道是否为诸如读写事件做好准备的组件.这样,一个单独的线程可以管理多个channel,从而管理多个网络连接. 下面是 ...

  4. NIO组件Selector工作机制详解(下)

    转自:http://blog.csdn.net/haoel/article/details/2224069 五.  迷惑不解 : 为什么要自己消耗资源? 令人不解的是为什么我们的Java的New I/ ...

  5. NIO的Selector

    参考自 Java NIO系列教程(六) Selector Java-NIO-Selector java.nio.channels.Selector NIO新功能Top 10(下) 出发点: 如何管理多 ...

  6. NIO 概述 与 通信实例

    NIO 简述: NIO是在jdk1.4之后加入的一种基于缓冲区(buffer)和通道(channel)的I/O方式, nio是同步非阻塞的i/o模式,同步是指线程不断地轮询i/o事件,非阻塞是在处理i ...

  7. Java NIO之Selector(选择器)

    历史回顾: Java NIO 概览 Java NIO 之 Buffer(缓冲区) Java NIO 之 Channel(通道) 其他高赞文章: 面试中关于Redis的问题看这篇就够了 一文轻松搞懂re ...

  8. Nio使用Selector客户端与服务器的通信

    使用NIO的一个最大优势就是客户端于服务器自己的不再是阻塞式的,也就意味着服务器无需通过为每个客户端的链接而开启一个线程.而是通过一个叫Selector的轮循器来不断的检测那个Channel有消息处理 ...

  9. WPF中实例化Com组件,调用组件的方法时报System.Windows.Forms.AxHost+InvalidActiveXStateException的异常

    WPF中实例化Com组件,调用组件的方法时报System.Windows.Forms.AxHost+InvalidActiveXStateException的异常 在wpf中封装Com组件时,调用组件 ...

随机推荐

  1. mongodb3.2系统性学习——2、write concern mongodb 写安全机制

    为了尊重作者原文章位置:http://kyfxbl.iteye.com/blog/1952941 首先讲一下mongodb 的写操作过程: mongodb有一个write concern的设置,作用是 ...

  2. JavaScript模块化开发库之SeaJS

    SeaJS是一个很好的前端模块化开发库,源码不到1500行,压缩后才4k,质量极高.

  3. 多目标遗传算法 ------ NSGA-II (部分源码解析)状态报告 打印 report.c

    /* Routines for storing population data into files */ # include <stdio.h> # include <stdlib ...

  4. Google java编程技术规范

    不遵循规范的程序猿,不是好的coder. 学习java有一段时间了,一直想找java编程技术规范来学习一下,幸而网络资源丰富,各路玩家乐于分享,省去了好多麻烦,姑且算站在网友的肩上,砥砺前行. /** ...

  5. Xcode报错:Unexpected '@' in program

    今天犯了个很弱的错误,就是当定义个一个@property时,编译器直接报错:Unexpected '@' in program 原因是把定义的属性写在.m文件中了,改到.h文件中就好了... 以后大家 ...

  6. Android 如何把一个 RelativeLayout或ImageView背景设为透明

    在项目中,需要把RelativeLayout 和  ImageView背景设置为透明,怎么实现呢?这里主要通过代码,请参阅以下关键代码: public ImageView imgDetail; pri ...

  7. javascript循环

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  8. for嵌套for ★

    namespace for嵌套for五角星{    class Program    {        static void Main(string[] args)        {         ...

  9. poj1741 bzoj2152

    树分治入门 poj1741是男人八题之一,经典的树分治的题目这里用到的是点分治核心思想是我们把某个点i作为根,把路径分为过点i和不过点i先统计过点i这样的路径数,然后在统计其子树中的答案,这样就不断地 ...

  10. FusionCharts参数说明-----3D饼图属性(Pie3D.swf )

    animation 是否显示加载图表时的动画 palette 内置的图表样式,共5个 paletteColors 自定义图表元素颜色(为多个,如过过少会重复) showAboutMenuItem 右键 ...