一、前言
我们已经学习了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); } }