网上找一段 java nio的常见代码,代码如下:
1 public static void main(String[] args) throws IOException {
2 Selector selector = Selector.open();
3 ServerSocketChannel ssc = ServerSocketChannel.open();
4 ssc.configureBlocking(false);
5 ssc.socket().bind(new InetSocketAddress(9527));
6 ssc.register(selector, SelectionKey.OP_ACCEPT);
7 while(true){
8 int n = selector.select();
9 if (n <= 0) continue;
10 Iterator it = selector.selectedKeys().iterator();
11 while(it.hasNext()){
12 SelectionKey key = (SelectionKey)it.next();
13 if (key.isAcceptable()){
14 SocketChannel sc= ((ServerSocketChannel) key.channel()).accept();
15 sc.configureBlocking(false);
16 sc.register(key.selector(), SelectionKey.OP_READ|SelectionKey.OP_WRITE);
17 }
18 if (key.isReadable()){
19 SocketChannel channel = ((SocketChannel) key.channel());
20 ByteBuffer bf = ByteBuffer.allocate(10);
21 int read = channel.read(bf);
22 System.out.println("read "+read+" : "+new String(bf.array()).trim());
23 }
24 if (key.isWritable()){
25 SocketChannel channel = ((SocketChannel) key.channel());
26 channel.write(ByteBuffer.wrap(new String("hello client").getBytes()));
27 }
28 it.remove();
29 }
30 }
31 }
这个是nio服务端的创建过程;第1行开启一个selector,第二行新建一个channel,然后将这个socketChannel注意到 selector当中;
while循环用来遍历selector当中的注册的事件;
channel结构如下:
主要分为file类的channel(file)与socket的channel(tcp),ByteChannel(udp);
现在再粗略看下selector,主要看下他的open(),源码如下:
/**
* Opens a selector.
*
* <p> The new selector is created by invoking the {@link
* java.nio.channels.spi.SelectorProvider#openSelector openSelector} method
* of the system-wide default {@link
* java.nio.channels.spi.SelectorProvider} object. </p>
*
* @return A new selector
*
* @throws IOException
* If an I/O error occurs
*/
public static Selector open() throws IOException {
return SelectorProvider.provider().openSelector();
}
看注释,表明是创建一下selector;一般服务端只有一个selector;粗略的架构图如下:
备注:其实selector可以有多个的;