JDK7 AIO初体验实例

原文地址:JDK7 AIO初体验实例作者:郑君华

JDK7已经release一段时间了,有个重要的新特性是AIO。
今天趁闲暇,简单体验了下,简单分享如下:

关于AIO的概念理解 
关于AIO的概念,仅谈谈个人的一点理解。可能不到位,请大家指出。
Io的两个重要步骤:发起IO请求,和实际的IO操作。在unix网络编程的定义里异步和非异步概念的区别就是实际的IO操作是否阻塞。如果不是就是异步,如果是就是同步。
而阻塞和非阻塞的区别在于发起IO请求的时候是否会阻塞,如果会就是阻塞,不会就是非阻塞。

本人理解能力有限,想了个例子来辅助自己理解:
小明想要买一本<深入java虚拟机>的书,以下几个场景可以来理解这几种io模式:
1.   如果小明每天都去书店问售货员说有没有这本书,如果没有就回去继续等待,等下次再过来文。(阻塞)
2.   如果小明告诉售货员想买一本<深入java虚拟机>的书,那么就在家里等着做其他事情去了,如果书到了售货员就通知小明,小明再自己过去取。
3.   如果小明告售货员想买一本<深入java虚拟机>的书,然后告诉售货员到了帮他送到某某地方去,就做其他事情去了。小明就不管了,等书到了,售货员就帮他送到那个地方了。


售货员可以认为是操作系统的一个服务,而小明是一个用户进程。不知道是否有误,如果有误请大家拍砖指出,谢谢。
可以看出2,3的效率明显要比1高。但是1最简单,而2,3需要一些协作。充分证明了团队合作的力量。

JDK7AIO初体验 
AsynchronousChannel:支持异步通道,包括服务端AsynchronousServerSocketChannel和普通AsynchronousSocketChannel等实现。
CompletionHandler:用户处理器。定义了一个用户处理就绪事件的接口,由用户自己实现,异步io的数据就绪后回调该处理器消费或处理数据。
AsynchronousChannelGroup:一个用于资源共享的异步通道集合。处理IO事件和分配给CompletionHandler。(具体这块还没细看代码,后续再分析这块)

以一个简单监听服务端为例,基本过程是: 
1.   启动一个服务端通道
2.   定义一个事件处理器,用户事件完成的时候处理,如消费数据。
3.   向系统注册一个感兴趣的事件,如接受数据,并把事件完成的处理器传递给系统。
4.   都已经交待完毕,可以只管继续做自己的事情了,操作系统在完成事件后通过其他的线程会自动调用处理器完成事件处理。

以下用一个例子来简单实现,一个服务端和客户端。服务端监听客户端的消息,并打印出来。

AIOServer.java

Java代码  收藏代码
  1. package io.aio;  
  2.   
  3. import java.io.IOException;  
  4. import java.net.InetSocketAddress;  
  5. import java.nio.ByteBuffer;  
  6. import java.nio.channels.AsynchronousServerSocketChannel;  
  7. import java.nio.channels.AsynchronousSocketChannel;  
  8. import java.nio.channels.CompletionHandler;  
  9. import java.util.concurrent.ExecutionException;  
  10. import java.util.concurrent.Future;  
  11. import java.util.concurrent.TimeUnit;  
  12. import java.util.concurrent.TimeoutException;  
  13.   
  14.   
  15. public class AIOServer {  
  16.     public final static int PORT = 9888;  
  17.     private AsynchronousServerSocketChannel server;  
  18.   
  19.     public AIOServer() throws IOException {  
  20.         server = AsynchronousServerSocketChannel.open().bind(  
  21.                 new InetSocketAddress(PORT));  
  22.     }  
  23.   
  24.     public void startWithFuture() throws InterruptedException,  
  25.             ExecutionException, TimeoutException {  
  26.         System.out.println("Server listen on " + PORT);  
  27.         Future<AsynchronousSocketChannel> future = server.accept();  
  28.         AsynchronousSocketChannel socket = future.get();  
  29.         ByteBuffer readBuf = ByteBuffer.allocate(1024);  
  30.         readBuf.clear();  
  31.         socket.read(readBuf).get(100, TimeUnit.SECONDS);  
  32.         readBuf.flip();  
  33.         System.out.printf("received message:" + new String(readBuf.array()));  
  34.         System.out.println(Thread.currentThread().getName());  
  35.   
  36.     }  
  37.   
  38.     public void startWithCompletionHandler() throws InterruptedException,  
  39.             ExecutionException, TimeoutException {  
  40.         System.out.println("Server listen on " + PORT);  
  41.         //注册事件和事件完成后的处理器  
  42.         server.accept(null,  
  43.                 new CompletionHandler<AsynchronousSocketChannel, Object>() {  
  44.                     final ByteBuffer buffer = ByteBuffer.allocate(1024);  
  45.   
  46.                     public void completed(AsynchronousSocketChannel result,  
  47.                             Object attachment) {  
  48.                         System.out.println(Thread.currentThread().getName());  
  49.                         System.out.println("start");  
  50.                         try {  
  51.                             buffer.clear();  
  52.                             result.read(buffer).get(100, TimeUnit.SECONDS);  
  53.                             buffer.flip();  
  54.                             System.out.println("received message: "  
  55.                                     + new String(buffer.array()));  
  56.                         } catch (InterruptedException | ExecutionException e) {  
  57.                             System.out.println(e.toString());  
  58.                         } catch (TimeoutException e) {  
  59.                             e.printStackTrace();  
  60.                         } finally {  
  61.   
  62.                             try {  
  63.                                 result.close();  
  64.                                 server.accept(nullthis);  
  65.                             } catch (Exception e) {  
  66.                                 System.out.println(e.toString());  
  67.                             }  
  68.                         }  
  69.   
  70.                         System.out.println("end");  
  71.                     }  
  72.   
  73.                     @Override  
  74.                     public void failed(Throwable exc, Object attachment) {  
  75.                         System.out.println("failed: " + exc);  
  76.                     }  
  77.                 });  
  78.         // 主线程继续自己的行为  
  79.         while (true) {  
  80.             System.out.println("main thread");  
  81.             Thread.sleep(1000);  
  82.         }  
  83.   
  84.     }  
  85.   
  86.     public static void main(String args[]) throws Exception {  
  87.         new AIOServer().startWithCompletionHandler();  
  88.     }  
  89. }  
 


AIOClient.java

Java代码  收藏代码
  1. package io.aio;  
  2.   
  3. import java.net.InetSocketAddress;  
  4. import java.nio.ByteBuffer;  
  5. import java.nio.channels.AsynchronousSocketChannel;  
  6.   
  7. public class AIOClient {  
  8.   
  9.     public static void main(String... args) throws Exception {  
  10.         AsynchronousSocketChannel client = AsynchronousSocketChannel.open();  
  11.         client.connect(new InetSocketAddress("localhost"9888));  
  12.         client.write(ByteBuffer.wrap("test".getBytes())).get();  
  13.     }  
  14. }  
 



服务端写了两种处理实现方式,startWithCompletionHandler是通过Handler来处理,startWithFuture是通过Future方式来处理。startWithCompletionHandler方法里可以看到调用accepte()完成异步注册后,线程就可以继续自己的处理了,完全不被这个io所中断。

从以上来看AIO的代码简单了很多,至少比NIO的代码实现简单很多。



原文地址:https://www.cnblogs.com/leeeee/p/7276570.html