JAVA NIO 例子

初识NIO:

    在 JDK 1. 4 中 新 加入 了 NIO( New Input/ Output) 类, 引入了一种基于通道和缓冲区的 I/O 方式,它可以使用 Native 函数库直接分配堆外内存,然后通过一个存储在 Java 堆的 DirectByteBuffer 对象作为这块内存的引用进行操作,避免了在 Java 堆和 Native 堆中来回复制数据。

    NIO 是一种同步非阻塞的 IO 模型。同步是指线程不断轮询 IO 事件是否就绪,非阻塞是指线程在等待 IO 的时候,可以同时做其他任务。同步的核心就是 Selector,Selector 代替了线程本身轮询 IO 事件,避免了阻塞同时减少了不必要的线程消耗;非阻塞的核心就是通道和缓冲区,当 IO 事件就绪时,可以通过写道缓冲区,保证 IO 的成功,而无需线程阻塞式地等待。

Buffer:

    为什么说NIO是基于缓冲区的IO方式呢?因为,当一个链接建立完成后,IO的数据未必会马上到达,为了当数据到达时能够正确完成IO操作,在BIO(阻塞IO)中,等待IO的线程必须被阻塞,以全天候地执行IO操作。为了解决这种IO方式低效的问题,引入了缓冲区的概念,当数据到达时,可以预先被写入缓冲区,再由缓冲区交给线程,因此线程无需阻塞地等待IO。

通道:

    当执行:SocketChannel.write(Buffer),便将一个 buffer 写到了一个通道中。如果说缓冲区还好理解,通道相对来说就更加抽象。网上博客难免有写不严谨的地方,容易使初学者感到难以理解。

    引用 Java NIO 中权威的说法:通道是 I/O 传输发生时通过的入口,而缓冲区是这些数 据传输的来源或目标。对于离开缓冲区的传输,您想传递出去的数据被置于一个缓冲区,被传送到通道。对于传回缓冲区的传输,一个通道将数据放置在您所提供的缓冲区中。

    例如 有一个服务器通道 ServerSocketChannel serverChannel,一个客户端通道 SocketChannel clientChannel;服务器缓冲区:serverBuffer,客户端缓冲区:clientBuffer。

    当服务器想向客户端发送数据时,需要调用:clientChannel.write(serverBuffer)。当客户端要读时,调用 clientChannel.read(clientBuffer)

    当客户端想向服务器发送数据时,需要调用:serverChannel.write(clientBuffer)。当服务器要读时,调用 serverChannel.read(serverBuffer)

    这样,通道和缓冲区的关系似乎更好理解了。在实践中,未必会出现这种双向连接的蠢事(然而这确实存在的,后面的内容还会涉及),但是可以理解为在NIO中:如果想将Data发到目标端,则需要将存储该Data的Buffer,写入到目标端的Channel中,然后再从Channel中读取数据到目标端的Buffer中。

Selector:

    通道和缓冲区的机制,使得线程无需阻塞地等待IO事件的就绪,但是总是要有人来监管这些IO事件。这个工作就交给了selector来完成,这就是所谓的同步。

    Selector允许单线程处理多个 Channel。如果你的应用打开了多个连接(通道),但每个连接的流量都很低,使用Selector就会很方便。

    要使用Selector,得向Selector注册Channel,然后调用它的select()方法。这个方法会一直阻塞到某个注册的通道有事件就绪,这就是所说的轮询。一旦这个方法返回,线程就可以处理这些事件。

    Selector中注册的感兴趣事件有:

  • OP_ACCEPT

  • OP_CONNECT 

  • OP_READ 

  • OP_WRITE

优化:

    一种优化方式是:将Selector进一步分解为Reactor,将不同的感兴趣事件分开,每一个Reactor只负责一种感兴趣的事件。这样做的好处是:1、分离阻塞级别,减少了轮询的时间;2、线程无需遍历set以找到自己感兴趣的事件,因为得到的set中仅包含自己感兴趣的事件。

NIO和epoll:

    epoll是Linux内核的IO模型。我想一定有人想问,AIO听起来比NIO更加高大上,为什么不使用AIO?AIO其实也有应用,但是有一个问题就是,Linux是不支持AIO的,因此基于AIO的程序运行在Linux上的效率相比NIO反而更低。而Linux是最主要的服务器OS,因此相比AIO,目前NIO的应用更加广泛。

    说到这里,可能你已经明白了,epoll一定和NIO有着很深的因缘。没错,如果仔细研究epoll的技术内幕,你会发现它确实和NIO非常相似,都是基于“通道”和缓冲区的,也有selector,只是在epoll中,通道实际上是操作系统的“管道”。和NIO不同的是,NIO中,解放了线程,但是需要由selector阻塞式地轮询IO事件的就绪;而epoll中,IO事件就绪后,会自动发送消息,通知selector:“我已经就绪了。”可以认为,Linux的epoll是一种效率更高的NIO。

NIO轶事:

    一篇有意思的博客,讲的 Java selector.open() 的时候,会创建一个自己和自己的链接(windows上是tcp,linux上是通道)

    这么做的原因:可以从 Apache Mina 中窥探。在 Mina 中,有如下机制:

  1. Mina框架会创建一个Work对象的线程。

  2. Work对象的线程的run()方法会从一个队列中拿出一堆Channel,然后使用Selector.select()方法来侦听是否有数据可以读/写。

  3. 最关键的是,在select的时候,如果队列有新的Channel加入,那么,Selector.select()会被唤醒,然后重新select最新的Channel集合。

  4. 要唤醒select方法,只需要调用Selector的wakeup()方法。

    而一个阻塞在select上的线程有以下三种方式可以被唤醒:

  1. 有数据可读/写,或出现异常。

  2. 阻塞时间到,即time out。

  3. 收到一个non-block的信号。可由kill或pthread_kill发出。

    首先 2 可以排除,而第三种方式,只在linux中存在。因此,Java NIO为什么要创建一个自己和自己的链接:就是如果想要唤醒select,只需要朝着自己的这个loopback连接发点数据过去,于是,就可以唤醒阻塞在select上的线程了。


Java代码   收藏代码
  1. import java.io.IOException;  
  2. import java.net.InetSocketAddress;  
  3. import java.net.ServerSocket;  
  4. import java.nio.ByteBuffer;  
  5. import java.nio.channels.SelectionKey;  
  6. import java.nio.channels.Selector;  
  7. import java.nio.channels.ServerSocketChannel;  
  8. import java.nio.channels.SocketChannel;  
  9. import java.util.Iterator;  
  10. import java.util.Set;  
  11.   
  12. public class NIOServer {  
  13.       
  14.     /*标识数字*/  
  15.     private  int flag = 0;  
  16.     /*缓冲区大小*/  
  17.     private  int BLOCK = 4096;  
  18.     /*接受数据缓冲区*/  
  19.     private  ByteBuffer sendbuffer = ByteBuffer.allocate(BLOCK);  
  20.     /*发送数据缓冲区*/  
  21.     private  ByteBuffer receivebuffer = ByteBuffer.allocate(BLOCK);  
  22.     private  Selector selector;  
  23.   
  24.     public NIOServer(int port) throws IOException {  
  25.         // 打开服务器套接字通道  
  26.         ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();  
  27.         // 服务器配置为非阻塞  
  28.         serverSocketChannel.configureBlocking(false);  
  29.         // 检索与此通道关联的服务器套接字  
  30.         ServerSocket serverSocket = serverSocketChannel.socket();  
  31.         // 进行服务的绑定  
  32.         serverSocket.bind(new InetSocketAddress(port));  
  33.         // 通过open()方法找到Selector  
  34.         selector = Selector.open();  
  35.         // 注册到selector,等待连接  
  36.         serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);  
  37.         System.out.println("Server Start----8888:");  
  38.     }  
  39.   
  40.   
  41.     // 监听  
  42.     private void listen() throws IOException {  
  43.         while (true) {  
  44.             // 选择一组键,并且相应的通道已经打开  
  45.             selector.select();  
  46.             // 返回此选择器的已选择键集。  
  47.             Set<SelectionKey> selectionKeys = selector.selectedKeys();  
  48.             Iterator<SelectionKey> iterator = selectionKeys.iterator();  
  49.             while (iterator.hasNext()) {          
  50.                 SelectionKey selectionKey = iterator.next();  
  51.                 iterator.remove();  
  52.                 handleKey(selectionKey);  
  53.             }  
  54.         }  
  55.     }  
  56.   
  57.     // 处理请求  
  58.     private void handleKey(SelectionKey selectionKey) throws IOException {  
  59.         // 接受请求  
  60.         ServerSocketChannel server = null;  
  61.         SocketChannel client = null;  
  62.         String receiveText;  
  63.         String sendText;  
  64.         int count=0;  
  65.         // 测试此键的通道是否已准备好接受新的套接字连接。  
  66.         if (selectionKey.isAcceptable()) {  
  67.             // 返回为之创建此键的通道。  
  68.             server = (ServerSocketChannel) selectionKey.channel();  
  69.             // 接受到此通道套接字的连接。  
  70.             // 此方法返回的套接字通道(如果有)将处于阻塞模式。  
  71.             client = server.accept();  
  72.             // 配置为非阻塞  
  73.             client.configureBlocking(false);  
  74.             // 注册到selector,等待连接  
  75.             client.register(selector, SelectionKey.OP_READ);  
  76.         } else if (selectionKey.isReadable()) {  
  77.             // 返回为之创建此键的通道。  
  78.             client = (SocketChannel) selectionKey.channel();  
  79.             //将缓冲区清空以备下次读取  
  80.             receivebuffer.clear();  
  81.             //读取服务器发送来的数据到缓冲区中  
  82.             count = client.read(receivebuffer);   
  83.             if (count > 0) {  
  84.                 receiveText = new String( receivebuffer.array(),0,count);  
  85.                 System.out.println("服务器端接受客户端数据--:"+receiveText);  
  86.                 client.register(selector, SelectionKey.OP_WRITE);  
  87.             }  
  88.         } else if (selectionKey.isWritable()) {  
  89.             //将缓冲区清空以备下次写入  
  90.             sendbuffer.clear();  
  91.             // 返回为之创建此键的通道。  
  92.             client = (SocketChannel) selectionKey.channel();  
  93.             sendText="message from server--" + flag++;  
  94.             //向缓冲区中输入数据  
  95.             sendbuffer.put(sendText.getBytes());  
  96.              //将缓冲区各标志复位,因为向里面put了数据标志被改变要想从中读取数据发向服务器,就要复位  
  97.             sendbuffer.flip();  
  98.             //输出到通道  
  99.             client.write(sendbuffer);  
  100.             System.out.println("服务器端向客户端发送数据--:"+sendText);  
  101.             client.register(selector, SelectionKey.OP_READ);  
  102.         }  
  103.     }  
  104.   
  105.     /** 
  106.      * @param args 
  107.      * @throws IOException 
  108.      */  
  109.     public static void main(String[] args) throws IOException {  
  110.         // TODO Auto-generated method stub  
  111.         int port = 8888;  
  112.         NIOServer server = new NIOServer(port);  
  113.         server.listen();  
  114.     }  
  115. }  
 
Java代码   收藏代码
  1. import java.io.IOException;  
  2. import java.net.InetSocketAddress;  
  3. import java.nio.ByteBuffer;  
  4. import java.nio.channels.SelectionKey;  
  5. import java.nio.channels.Selector;  
  6. import java.nio.channels.SocketChannel;  
  7. import java.util.Iterator;  
  8. import java.util.Set;  
  9.   
  10. public class NIOClient {  
  11.   
  12.     /*标识数字*/  
  13.     private static int flag = 0;  
  14.     /*缓冲区大小*/  
  15.     private static int BLOCK = 4096;  
  16.     /*接受数据缓冲区*/  
  17.     private static ByteBuffer sendbuffer = ByteBuffer.allocate(BLOCK);  
  18.     /*发送数据缓冲区*/  
  19.     private static ByteBuffer receivebuffer = ByteBuffer.allocate(BLOCK);  
  20.     /*服务器端地址*/  
  21.     private final static InetSocketAddress SERVER_ADDRESS = new InetSocketAddress(  
  22.             "localhost"1111);  
  23.   
  24.     public static void main(String[] args) throws IOException {  
  25.         // TODO Auto-generated method stub  
  26.         // 打开socket通道  
  27.         SocketChannel socketChannel = SocketChannel.open();  
  28.         // 设置为非阻塞方式  
  29.         socketChannel.configureBlocking(false);  
  30.         // 打开选择器  
  31.         Selector selector = Selector.open();  
  32.         // 注册连接服务端socket动作  
  33.         socketChannel.register(selector, SelectionKey.OP_CONNECT);  
  34.         // 连接  
  35.         socketChannel.connect(SERVER_ADDRESS);  
  36.         // 分配缓冲区大小内存  
  37.           
  38.         Set<SelectionKey> selectionKeys;  
  39.         Iterator<SelectionKey> iterator;  
  40.         SelectionKey selectionKey;  
  41.         SocketChannel client;  
  42.         String receiveText;  
  43.         String sendText;  
  44.         int count=0;  
  45.   
  46.         while (true) {  
  47.             //选择一组键,其相应的通道已为 I/O 操作准备就绪。  
  48.             //此方法执行处于阻塞模式的选择操作。  
  49.             selector.select();  
  50.             //返回此选择器的已选择键集。  
  51.             selectionKeys = selector.selectedKeys();  
  52.             //System.out.println(selectionKeys.size());  
  53.             iterator = selectionKeys.iterator();  
  54.             while (iterator.hasNext()) {  
  55.                 selectionKey = iterator.next();  
  56.                 if (selectionKey.isConnectable()) {  
  57.                     System.out.println("client connect");  
  58.                     client = (SocketChannel) selectionKey.channel();  
  59.                     // 判断此通道上是否正在进行连接操作。  
  60.                     // 完成套接字通道的连接过程。  
  61.                     if (client.isConnectionPending()) {  
  62.                         client.finishConnect();  
  63.                         System.out.println("完成连接!");  
  64.                         sendbuffer.clear();  
  65.                         sendbuffer.put("Hello,Server".getBytes());  
  66.                         sendbuffer.flip();  
  67.                         client.write(sendbuffer);  
  68.                     }  
  69.                     client.register(selector, SelectionKey.OP_READ);  
  70.                 } else if (selectionKey.isReadable()) {  
  71.                     client = (SocketChannel) selectionKey.channel();  
  72.                     //将缓冲区清空以备下次读取  
  73.                     receivebuffer.clear();  
  74.                     //读取服务器发送来的数据到缓冲区中  
  75.                     count=client.read(receivebuffer);  
  76.                     if(count>0){  
  77.                         receiveText = new String( receivebuffer.array(),0,count);  
  78.                         System.out.println("客户端接受服务器端数据--:"+receiveText);  
  79.                         client.register(selector, SelectionKey.OP_WRITE);  
  80.                     }  
  81.   
  82.                 } else if (selectionKey.isWritable()) {  
  83.                     sendbuffer.clear();  
  84.                     client = (SocketChannel) selectionKey.channel();  
  85.                     sendText = "message from client--" + (flag++);  
  86.                     sendbuffer.put(sendText.getBytes());  
  87.                      //将缓冲区各标志复位,因为向里面put了数据标志被改变要想从中读取数据发向服务器,就要复位  
  88.                     sendbuffer.flip();  
  89.                     client.write(sendbuffer);  
  90.                     System.out.println("客户端向服务器端发送数据--:"+sendText);  
  91.                     client.register(selector, SelectionKey.OP_READ);  
  92.                 }  
  93.             }  
  94.             selectionKeys.clear();  
  95.         }  
  96.     }  
  97. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值