[编织消息框架][网络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左右连接(后面会讲如何突破)
转载于:https://www.cnblogs.com/solq111/p/6744489.html
[编织消息框架][网络IO模型]AIO相关推荐
- 五种网络IO模型详解
一 IO操作本质 数据复制的过程中不会消耗CPU # 1 内存分为内核缓冲区和用户缓冲区 # 2 用户的应用程序不能直接操作内核缓冲区,需要将数据从内核拷贝到用户才能使用 # 3 而IO操作.网络请求 ...
- C++后台开发—网络IO模型与Reactor模式
一.三种网络IO模型: 分类: BIO 同步的.阻塞式 IO NIO 同步的.非阻塞式 IO AIO 异步非阻塞式 IO 推荐视频: C++架构师学习地址:C/C++Linux服务器开发高级架构师/L ...
- 大白话详解5种网络IO模型
1 前言 我们都知道,为了实现高性能的通信服务器,BIO在高并发的情况下会出现性能急剧下降的问题,甚至会由于创建过多线程而导致系统OOM.因此在Java业界,BIO的性能问题一直被开发者所诟病,所幸的 ...
- 网络IO模型的深入浅出
标题索引 追溯IO原因 网络数据流 网络IO模型 IO模型举例 追溯IO原因 从事项目多年来,有个问题一直困扰着我,但因种种原因一直没有翻阅资料去释怀,随着项目经历的增加.年龄的增长和责任的使 ...
- Linux 网络 IO 模型
写在前面 本文主要介绍 Unix/Linux 下五种网络 IO 模型,但是.为了更好的理解下面提到的五种网络 IO 的概念,我们有必要先理清下面这几个概念. 用户空间与内核空间 一个计算机通常有一定大 ...
- 5种网络IO模型介绍
5种网络IO模型介绍 IO 模型分为以下几种: 阻塞IO 非阻塞IO 信号驱动IO IO多路复用 异步IO 前四个为同步IO 1 阻塞IO 一个IO操作需要两步: 等待数据和拷贝数据. blockin ...
- linux 网络io命令详解,Linux下五种网络IO模型详解
本文我们主要来了解一下Unix/Linux下5种网络IO模型:blocking IO, nonblocking IO, IO multiplexing, signal driven IO, async ...
- 因为取了个快递我搞懂了五种网络IO模型
五种网络IO模型 目录 前段时间,我有个朋友因为拿快递和家里闹别扭了,今天我就借这事来讲讲五大网络模型 阻塞IO模型 第一天: 刘:今天因为拿快递被我妈骂了一顿 我:说来听听 刘:我本来在家里打扫家务 ...
- 网络模型——四种常见网络IO模型
文章目录 1.IO读写原理 1.1 内核缓冲区和进程缓存区 1.1.1 用户进程和操作系统 1.1.2 缓冲区的目的 1.2 Java读写IO底层流程 2.四种主要的IO模型 2.1 基本概念 2.1 ...
最新文章
- jQuery 性能优化技巧
- Javaweb-AJAX快速入门及案例实战
- [转载] Java-forEach增强for循环是值传递规则详解
- 【Python3网络爬虫开发实战】1.6.1-Flask的安装
- mtk2503 如何支持使用epo?
- 低级格式化(U盘可用空间越来越小解决方案)
- 关于CSS Reset 那些事(一)之 历史演变与Normalize.css
- 简单学习一下ibd数据文件解析
- 个人电脑php网站搭建,如何在本地电脑搭建自己网站的流程(图文教程)
- 计算机图形学矩形_《交互式计算机图形学》读书笔记 —— 第一章、图形系统和模型...
- 中划线与下划线的区别
- [七七黎]乱七八糟-美女和野兽
- 桌面打开计算机没反应,点击显示桌面没反应? 显示桌面没反应解决方法
- 三菱CC-link IE field basic 控制伺服轴
- 拓扑排序在实际项目中应用
- java添加坚挺_Java连载136-两种方式插入数据
- D - National Railway (DP)
- WolframAlpha
- PyG文档之一:安装
- word文档中如何删除空白页
热门文章
- aes默认填充算法 mysql_Go 实现加密算法系列之对称加密
- mybatis oracle生成注释,MyBatis Generator生成Oracle数据库对应实体类时无法获取注释问题...
- YOLOX目标检测模型Keras实现,超越Yolov5
- 如何使用sklearn进行数据挖掘?
- moment转换时间戳_酷炫时间轮盘:JS元素圆形布局制作时间轮盘动画效果
- axure小程序模板_公众号和小程序模板消息
- leetcode两数之和,三数之和,四数之和问题
- c语言popen函数多线程,关于多线程:多线程环境中的Python-Subprocess-Popen行为不一致...
- 服务器负载不高 响应慢_通俗易懂的讲透:负载均衡的原理
- Android仿人人客户端(v5.7.1)——采用ViewGroup做父容器,实现左侧滑动菜单(三)...