[编织消息框架][网络IO模型]AIO
asynchronous I/O (the POSIX aio_functions)—————异步IO模型最大的特点是 完成后发回通知。
与NIO不同,当进行读写操作时,只须直接调用API的read或write方法即可。这两种方法均为异步的,对于读操作而言,当有流可读取时,操作系统会将可读的流传入read方法的缓冲区,并通知应用程序;对于写操作而言,当操作系统将write方法传递的流写入完毕时,操作系统主动通知应用程序。
即可以理解为,accept,connect,read,write方法都是异步的,完成后会主动调用回调函数。
在JDK1.7中,这部分内容被称作NIO.2,主要在java.nio.channels包下增加了下面四个异步通道:
- AsynchronousSocketChannel
- AsynchronousServerSocketChannel
- AsynchronousFileChannel
- AsynchronousDatagramChannel
其中的accept,connect,read,write方法,会返回一个带回调函数的对象,当执行完读取/写入操作后,直接调用回调函数。
1 public class ClientAio implements Runnable { 2 private final static int count = 50000; 3 private final static AsynchronousSocketChannel[] clients = new AsynchronousSocketChannel[count]; 4 private final static AtomicInteger ai = new AtomicInteger(); 5 6 private String host; 7 private int port; 8 private AsynchronousSocketChannel client; 9 10 public ClientAio(String host, int port) throws IOException { 11 this.client = AsynchronousSocketChannel.open(); 12 this.host = host; 13 this.port = port; 14 } 15 16 public static void main(String[] args) throws Exception { 17 String addr = args.length > 0 ? args[0] : "192.168.56.122"; 18 while (ai.get() < count) { 19 new ClientAio(addr, 8989).run(); 20 } 21 System.in.read(); 22 } 23 24 public void run() { 25 client.connect(new InetSocketAddress(host, port), null, new CompletionHandler<Void, Object>() { 26 public void completed(Void result, Object attachment) { 27 int i = ai.getAndIncrement(); 28 if (i < count) { 29 clients[i] = client; 30 } 31 final ByteBuffer byteBuffer = ByteBuffer.allocate(512); 32 client.read(byteBuffer, null, new CompletionHandler<Integer, Object>() { 33 public void completed(Integer result, Object attachment) { 34 35 //System.out.println("client read data: " + new String(byteBuffer.array())); 36 } 37 38 public void failed(Throwable exc, Object attachment) { 39 System.out.println("read faield"); 40 } 41 }); 42 } 43 44 public void failed(Throwable exc, Object attachment) { 45 System.out.println("client connect field..."); 46 try { 47 if(client.isOpen()){ 48 client.close(); 49 } 50 } catch (IOException e) { 51 } 52 } 53 }); 54 } 55 }
1 public class ServerAio implements Runnable { 2 private final static AtomicInteger ai = new AtomicInteger(); 3 private int port = 8889; 4 private int threadSize = 10; 5 private AsynchronousChannelGroup asynchronousChannelGroup; 6 private AsynchronousServerSocketChannel serverChannel; 7 8 public ServerAio(int port, int threadSize) { 9 this.port = port; 10 this.threadSize = threadSize; 11 } 12 13 public static void main(String[] args) throws IOException { 14 new ServerAio(8989, Runtime.getRuntime().availableProcessors() * 10).run(); 15 System.in.read(); 16 } 17 18 public void run() { 19 try { 20 asynchronousChannelGroup = AsynchronousChannelGroup.withCachedThreadPool(Executors.newCachedThreadPool(), threadSize); 21 serverChannel = AsynchronousServerSocketChannel.open(asynchronousChannelGroup); 22 serverChannel.bind(new InetSocketAddress(port)); 23 System.out.println("listening on port: " + port); 24 serverChannel.accept(this, new CompletionHandler<AsynchronousSocketChannel, ServerAio>() { 25 26 public void completed(AsynchronousSocketChannel result, ServerAio attachment) { 27 try { 28 System.out.println(ai.getAndIncrement()); 29 ByteBuffer echoBuffer = ByteBuffer.allocateDirect(1024); 30 result.read(echoBuffer, null, new CompletionHandler<Integer, Object>() { 31 @Override 32 public void completed(Integer result, Object attachment) { 33 // System.out.println("received : " + 34 // Charset.defaultCharset().decode(echoBuffer)); 35 } 36 37 @Override 38 public void failed(Throwable exc, Object attachment) { 39 } 40 }); 41 42 result.write(ByteBuffer.wrap("ok".getBytes())); 43 } catch (Exception e) { 44 e.printStackTrace(); 45 } finally { 46 attachment.serverChannel.accept(attachment, this);// 监听新的请求,递归调用。 47 } 48 } 49 50 public void failed(Throwable exc, ServerAio attachment) { 51 System.out.println("received failed"); 52 exc.printStackTrace(); 53 attachment.serverChannel.accept(attachment, this);// 监听新的请求,递归调用。 54 } 55 }); 56 57 } catch (Exception e) { 58 e.printStackTrace(); 59 } 60 } 61 }
AIO与NIO对比,减少read阻塞等侍时间,速度非常之快,本人在window环境下测试瓶颈1.6W左右连接(后面会讲如何突破)
作者: | solq |
博客地址: | http://www.cnblogs.com/solq111 |
博客版权: | 本文以学习、研究和分享为主,欢迎转载,但必须在文章页面明显位置给出原文连接。 如果文中有不妥或者错误的地方还望高手的你指出,以免误人子弟。如果觉得本文对你有所帮助不如【推荐】一下!如果你有更好的建议,不如留言一起讨论,共同进步! 再次感谢您耐心的读完本篇文章。 淘宝店: 海豚极货店 QQ群:9547527 |
如果你热爱生活、热爱编程、热爱吉他。扫一扫加我微信 |
我的新书《编织消息框架》目前进行中,感谢大家关注! |
本作品采用知识共享署名-非商业性使用-相同方式共享 2.5 中国大陆许可协议进行许可。 |