[编织消息框架][网络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方法,会返回一个带回调函数的对象,当执行完读取/写入操作后,直接调用回调函数。
public class ClientAio implements Runnable {
private final static int count = 50000;
private final static AsynchronousSocketChannel[] clients = new AsynchronousSocketChannel[count];
private final static AtomicInteger ai = new AtomicInteger();
private String host;
private int port;
private AsynchronousSocketChannel client;
public ClientAio(String host, int port) throws IOException {
this.client = AsynchronousSocketChannel.open();
this.host = host;
this.port = port;
}
public static void main(String[] args) throws Exception {
String addr = args.length > 0 ? args[0] : "192.168.56.122";
while (ai.get() < count) {
new ClientAio(addr, 8989).run();
}
System.in.read();
}
public void run() {
client.connect(new InetSocketAddress(host, port), null, new CompletionHandler<Void, Object>() {
public void completed(Void result, Object attachment) {
int i = ai.getAndIncrement();
if (i < count) {
clients[i] = client;
}
final ByteBuffer byteBuffer = ByteBuffer.allocate(512);
client.read(byteBuffer, null, new CompletionHandler<Integer, Object>() {
public void completed(Integer result, Object attachment) {
//System.out.println("client read data: " + new String(byteBuffer.array()));
}
public void failed(Throwable exc, Object attachment) {
System.out.println("read faield");
}
});
}
public void failed(Throwable exc, Object attachment) {
System.out.println("client connect field...");
try {
if(client.isOpen()){
client.close();
}
} catch (IOException e) {
}
}
});
}
}
public class ServerAio implements Runnable {
private final static AtomicInteger ai = new AtomicInteger();
private int port = 8889;
private int threadSize = 10;
private AsynchronousChannelGroup asynchronousChannelGroup;
private AsynchronousServerSocketChannel serverChannel;
public ServerAio(int port, int threadSize) {
this.port = port;
this.threadSize = threadSize;
}
public static void main(String[] args) throws IOException {
new ServerAio(8989, Runtime.getRuntime().availableProcessors() * 10).run();
System.in.read();
}
public void run() {
try {
asynchronousChannelGroup = AsynchronousChannelGroup.withCachedThreadPool(Executors.newCachedThreadPool(), threadSize);
serverChannel = AsynchronousServerSocketChannel.open(asynchronousChannelGroup);
serverChannel.bind(new InetSocketAddress(port));
System.out.println("listening on port: " + port);
serverChannel.accept(this, new CompletionHandler<AsynchronousSocketChannel, ServerAio>() {
public void completed(AsynchronousSocketChannel result, ServerAio attachment) {
try {
System.out.println(ai.getAndIncrement());
ByteBuffer echoBuffer = ByteBuffer.allocateDirect(1024);
result.read(echoBuffer, null, new CompletionHandler<Integer, Object>() {
@Override
public void completed(Integer result, Object attachment) {
// System.out.println("received : " +
// Charset.defaultCharset().decode(echoBuffer));
}
@Override
public void failed(Throwable exc, Object attachment) {
}
});
result.write(ByteBuffer.wrap("ok".getBytes()));
} catch (Exception e) {
e.printStackTrace();
} finally {
attachment.serverChannel.accept(attachment, this);// 监听新的请求,递归调用。
}
}
public void failed(Throwable exc, ServerAio attachment) {
System.out.println("received failed");
exc.printStackTrace();
attachment.serverChannel.accept(attachment, this);// 监听新的请求,递归调用。
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
}
AIO与NIO对比,减少read阻塞等侍时间,速度非常之快,本人在window环境下测试瓶颈1.6W左右连接(后面会讲如何突破)
[编织消息框架][网络IO模型]aio的更多相关文章
- [编织消息框架][网络IO模型]BIO
既然跟网络内容有关就不得不学习网络IO模型,时代在进步,技术也在进步,采取使用那种网络IO模型就已经确定应用程序规模 阻塞IO(blocking IO) 在linux中,默认情况下所有的socket都 ...
- [编织消息框架][网络IO模型]NIO(select and poll)
上面测试论证系统内核在read data时会阻塞,如果我们在把第一个阶段解决掉那么性能就会提高 NIO 编程 JDK 1.4中的java.nio.*包中引入新的Java I/O库,其目的是提高速度.实 ...
- [编织消息框架][网络IO模型]Netty Reactor
严格来讲Netty Reactor是一种设计模式,一听模式两字就知道了吧,套路哈哈 Reactor中文译为“反应堆”. 看图netty处理流程 1.netty server 至少有两组reactor. ...
- 打开APP 04 | 网络通信:RPC框架在网络通信上更倾向于哪种网络IO模型? 2020-02-26 何小锋
打开APP 04 | 网络通信:RPC框架在网络通信上更倾向于哪种网络IO模型? 2020-02-26 何小锋
- Socket-IO 系列(一)Linux 网络 IO 模型
Socket-IO 系列(一)Linux 网络 IO 模型 一.基本概念 在正式开始讲 Linux IO 模型前,先介绍 5 个基本概念. 1.1 用户空间与内核空间 现在操作系统都是采用虚拟存储器, ...
- 网络IO模型与Reactor模式
一.三种网络IO模型: 分类: BIO 同步的.阻塞式 IO NIO 同步的.非阻塞式 IO AIO 异步非阻塞式 IO 阻塞和同步的概念: 阻塞:若读写未完成,调用读写的线程一直等待 非阻塞:若读写 ...
- 通过实例理解Java网络IO模型
网络IO模型及分类 网络IO模型是一个经常被提到的问题,不同的书或者博客说法可能都不一样,所以没必要死抠字眼,关键在于理解. Socket连接 不管是什么模型,所使用的socket连接都是一样的. 以 ...
- 五种网络IO模型以及多路复用IO中select/epoll对比
下面都是以网络读数据为例 [2阶段网络IO] 第一阶段:等待数据 wait for data 第二阶段:从内核复制数据到用户 copy data from kernel to user 下面是5种网络 ...
- Unix 网络IO模型介绍
带着问题阅读 1.什么是同步异步.阻塞非阻塞 2.有几种IO模型,不同模型之间有什么区别 3.不同IO模型的应用场景都是什么 同步和异步.阻塞和非阻塞 同步和异步 广义上讲同步异步描述的是事件中发送方 ...
随机推荐
- FineUIMvc随笔(6)对比WebForms和MVC中表格的数据库分页
声明:FineUIMvc(基础版)是免费软件,本系列文章适用于基础版. 通过对比WebForms和MVC中表格数据库分页代码的不同,可以对 MVC 中的数据流转有更加深入的了解. WebForms 中 ...
- Markdown语法讲解及MWeb使用教程
写了一个月的博客,忽然感觉Markdown编辑器比较好用,于是就下载了一个本地的Markdown编辑软件学习了一下,刚好软件里自带了一篇英文的指示文档,顺便翻译了一下,通过这个过程也大致熟悉了Mark ...
- .Net程序员学用Oracle系列(22):分析函数(OVER)
1.函数语法 1.1.语法概述 1.2.窗口详解 1.2.1.ROWS 窗口 1.2.2.RANGE 窗口 2.函数用法 2.1.普通统计类函数 2.2.数据排序类函数 2.3.数据分布类函数 2.4 ...
- git中常见的几个命令
git中常见的几个命令 本地仓库 三个区域 工作目录 暂存区 本地仓库 文件的四个状态 未跟踪 untracked 已暂存 staged 已提交commited 已修改 modified 基本命令 g ...
- 对VC++6.0爱得深沉(一)安装vc++6.0,支持winXP,win7,win8.1,win10
[欢迎入坑] 从这里起,我称VC++6.0为小c. 为什么我对小c爱得深沉? 虽然饱受非议,但是,我只想说,我太单纯,小c轻轻松松成功运行在各个win平台,对于我来说她:高速.小巧.便捷.听话.可定制 ...
- ICC_lab总结——ICC_lab3:布局
PS:字丑,禁止转载. 做到了ICC workshop的第三个实验,理论与实践相结合,于是,先放一张总结图,这张总结图来自前面的博客,放在这里用来体现理论和实践的联系: 这个就是布局的理论部分,在IC ...
- 解决MVC中JsonResult返回 弹出文件下载对话框
设置一下返回类型为HTML TEXT就可以了 JsonResult json = Json(xxx, JsonRequestBehavior.DenyGet); json.ContentType = ...
- Storm(2015.08.12笔记)
2015.08.12Storm 一.Storm简介 Storm是Twitter开源的一个类似于Hadoop的实时数据处理框架. Storm能实现高频数据和大规模数据的实时处理. 官网资料显示s ...
- JavaEE开发之Spring中的依赖注入与AOP
上篇博客我们系统的聊了<JavaEE开发之基于Eclipse的环境搭建以及Maven Web App的创建>,并在之前的博客中我们聊了依赖注入的相关东西,并且使用Objective-C的R ...
- 走入PHP-declare、ticks、encoding、include
declare 结构用来设定一段代码的执行指令.declare 的语法和其它流程控制结构相似(该代码为语法格式,不是代码案例,无需敲打该代码): declare (directive) stateme ...