JAVA并发处理经验(四)并行模式与算法7:AIO网络编程
来源:互联网 发布:python 爬虫书 推荐 编辑:程序博客网 时间:2024/05/17 12:56
一、前言
我们已经学习了NIO是网络操作,提供了选择器selector阻塞操作,但是比较还是IO同步的。我等等IO准备好之后,得到通知,在进行IO操作。那么什么是AIO:Asynchronized;
那么AIo就是一个异步操作。---理解为:读完了再来通知我;我们业务逻辑变为回调函数,等等IO操作完成,由系统触发;
二、AIO
2.1服务端
package pattern.aio;import pattern.nio.NioServer;import java.io.IOException;import java.net.InetSocketAddress;import java.nio.ByteBuffer;import java.nio.channels.AsynchronousServerSocketChannel;import java.nio.channels.AsynchronousSocketChannel;import java.nio.channels.ByteChannel;import java.nio.channels.CompletionHandler;import java.nio.channels.spi.AbstractInterruptibleChannel;import java.util.concurrent.ExecutionException;import java.util.concurrent.Future;import java.util.concurrent.TimeUnit;import java.util.concurrent.TimeoutException;/** * Created by ycy on 16/1/21. */public class AIOServer {//首先需要适用异步通道 public final static int PORT=65500; private AsynchronousServerSocketChannel server; public AIOServer() throws IOException { server=AsynchronousServerSocketChannel.open().bind(new InetSocketAddress(PORT)); }public void start(){ System.out.println("Server listen on" +PORT); //注册事件和事件完成过后的处理器 server.accept(null, new CompletionHandler<AsynchronousSocketChannel, Object>() { public void completed(AsynchronousSocketChannel result, Object attachment) { final ByteBuffer buffer=ByteBuffer.allocate(1024); System.out.println(Thread.currentThread().getName()); Future<Integer> writeResult=null; try{ result.read(buffer).get(100, TimeUnit.SECONDS); buffer.flip(); writeResult=result.write(buffer); }catch (InterruptedException|ExecutionException e){ e.printStackTrace(); }catch (TimeoutException e){ e.printStackTrace(); }finally { try { server.accept(null,this); writeResult.get(); result.close(); }catch (Exception e){ e.printStackTrace(); } } } public void failed(Throwable exc, Object attachment) { System.out.println("failed:"+exc); } });} public static void main(String[] args) throws IOException, InterruptedException { new AIOServer().start(); while (true){ Thread.sleep(1000); } }}
2.2 客户端
package pattern.aio;import java.io.IOException;import java.net.InetSocketAddress;import java.nio.ByteBuffer;import java.nio.channels.AsynchronousServerSocketChannel;import java.nio.channels.AsynchronousSocketChannel;import java.nio.channels.CompletionHandler;/** * Created by ycy on 16/1/21. */public class AIOClient { public static void main(String[] args) throws IOException, InterruptedException { final AsynchronousSocketChannel channel=AsynchronousSocketChannel.open(); channel.connect(new InetSocketAddress("127.0.0.1", 65500), null, new CompletionHandler<Void, Object>() { @Override public void completed(Void result, Object attachment) { try { final ByteBuffer buffer=ByteBuffer.allocate(1024); channel.read(buffer, buffer, new CompletionHandler<Integer, ByteBuffer>() { @Override public void completed(Integer result, ByteBuffer attachment) { buffer.flip(); System.out.println(new String(buffer.array())); try{ channel.close(); }catch (IOException e){ e.printStackTrace(); } } @Override public void failed(Throwable exc, ByteBuffer attachment) { } }); }catch (Exception e){ e.printStackTrace(); } } @Override public void failed(Throwable exc, Object attachment) { } }); //主线程结束,这里等待 上时速处理全部完成 Thread.sleep(1000); }}
0 0
- JAVA并发处理经验(四)并行模式与算法7:AIO网络编程
- JAVA并发处理经验(四)并行模式与算法6:NIO网络编程
- JAVA并发处理经验(四)并行模式与算法3:并行流水线模式
- JAVA并发处理经验(四)并行模式与算法4:并行搜索模式
- JAVA并发处理经验(四)并行模式与算法5:并行排序模式-奇偶性排序
- JAVA并发处理经验(四)并行模式与算法5:并行排序模式-希尔排序
- JAVA并发处理经验(四)并行模式与算法1:生产者消费与单例模式
- JAVA并发处理经验(四)并行模式与算法2:Future模式
- JAVA并发处理经验(四)并行模式与算法6:socket的服务端多线程
- 《Java高并发程序设计》总结--5. 并行模式与算法
- Java并发编程四:并发(Concurrent)与并行(Parallel)区别(二)
- 网络编程复习(四):AIO
- 并行模式与算法
- java网络编程(四)----异步非阻塞aio及proactor模型
- 并发并行与Go并发编程
- 《Java高并发程序设计》学习 --5.9 并行模式之并行算法:矩阵乘法
- java多线程与并发之java并发编程实践(四)
- JAVA并发处理经验(三)锁的优化与注意
- 二叉树学习(一)
- Linux用户态程序计时方式详解
- Java取整函数 四舍五入函数
- Android Studio 断点调试(debug)
- xwiki7.X版本升级日志
- JAVA并发处理经验(四)并行模式与算法7:AIO网络编程
- 理解以太坊 Serenity - 第二部分: Casper
- Excel图表制作(一):商务图表之甘特图
- Windows系统使用Tcl Expect Telnet 系统Crash解决方案
- MYSQL存储过程的一个完整实例
- duilib 界面库资料收集
- 后续博客移至 简书 http://www.jianshu.com/users/97cf7df43152/timeline
- win8.1下 cocos2dx-js 多线程http请求的问题
- ms-include与ms-include-src