IO模型 | BIO | NIO |
通訊 | 面向流(鄉村公路) | 面向緩衝(高速公路,多路複用技術) |
處理 | 阻塞 IO(多線程) | 非阻塞 IO(反應堆 Reactor) |
觸發 | 無 | 選擇器(輪詢機制) |
Name:Tom Age:18 Email: tom@qq.com Phone:13888888888
FileInputStream input = new FileInputStream("d://info.txt"); BufferedReader reader = new BufferedReader(new InputStreamReader(input)); String nameLine = reader.readLine(); String ageLine = reader.readLine(); String emailLine = reader.readLine(); String phoneLine = reader.readLine();
ByteBuffer buffer = ByteBuffer.allocate(48); int bytesRead = inChannel.read(buffer);
ByteBuffer buffer = ByteBuffer.allocate(48); int bytesRead = inChannel.read(buffer); while(!bufferFull(bytesRead)) { bytesRead = inChannel.read(buffer); }
package com.gupaoedu.vip.netty.io.aio; import java.io.IOException; import java.net.InetSocketAddress; import java.nio.ByteBuffer; import java.nio.channels.AsynchronousChannelGroup; import java.nio.channels.AsynchronousServerSocketChannel; import java.nio.channels.AsynchronousSocketChannel; import java.nio.channels.CompletionHandler; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; /** * AIO 服務端 */ public class AIOServer { private final int port; public static void main(String args[]) { int port = 8000; new AIOServer(port); } public AIOServer(int port) { this.port = port; listen(); } private void listen() { try { ExecutorService executorService = Executors.newCachedThreadPool(); AsynchronousChannelGroup threadGroup = AsynchronousChannelGroup.withCachedThreadPool(executorService, 1); final AsynchronousServerSocketChannel server = AsynchronousServerSocketChannel.open(threadGroup); server.bind(new InetSocketAddress(port)); System.out.println("服務已啓動,監聽端口" + port); server.accept(null, new CompletionHandler<AsynchronousSocketChannel, Object>(){ final ByteBuffer buffer = ByteBuffer.allocateDirect(1024); public void completed(AsynchronousSocketChannel result, Object attachment){ System.out.println("IO 操做成功,開始獲取數據"); try { buffer.clear(); result.read(buffer).get(); buffer.flip(); result.write(buffer); buffer.flip(); } catch (Exception e) { System.out.println(e.toString()); } finally { try { result.close(); server.accept(null, this); } catch (Exception e) { System.out.println(e.toString()); } } System.out.println("操做完成"); } @Override public void failed(Throwable exc, Object attachment) { System.out.println("IO 操做是失敗: " + exc); } }); try { Thread.sleep(Integer.MAX_VALUE); } catch (InterruptedException ex) { System.out.println(ex); } } catch (IOException e) { System.out.println(e); } } }
package com.gupaoedu.vip.netty.io.aio; import java.net.InetSocketAddress; import java.nio.ByteBuffer; import java.nio.channels.AsynchronousSocketChannel; import java.nio.channels.CompletionHandler; /** * AIO 客戶端 */ public class AIOClient { private final AsynchronousSocketChannel client; public AIOClient() throws Exception{ client = AsynchronousSocketChannel.open(); } public void connect(String host,int port)throws Exception{ client.connect(new InetSocketAddress(host,port),null,new CompletionHandler<Void,Void>() { @Override public void completed(Void result, Void attachment) { try { client.write(ByteBuffer.wrap("這是一條測試數據".getBytes())).get(); System.out.println("已發送至服務器"); } catch (Exception ex) { ex.printStackTrace(); } } @Override public void failed(Throwable exc, Void attachment) { exc.printStackTrace(); } }); final ByteBuffer bb = ByteBuffer.allocate(1024); client.read(bb, null, new CompletionHandler<Integer,Object>(){ @Override public void completed(Integer result, Object attachment) { System.out.println("IO 操做完成" + result); System.out.println("獲取反饋結果" + new String(bb.array())); } @Override public void failed(Throwable exc, Object attachment) { exc.printStackTrace(); } }); try { Thread.sleep(Integer.MAX_VALUE); } catch (InterruptedException ex) { System.out.println(ex); } } public static void main(String args[])throws Exception{ new AIOClient().connect("localhost",8000); } }
屬性 | 同步阻塞 IO(BIO) | 僞異步 IO | 非阻塞 IO(NIO) | 異步 IO(AIO) |
客戶端數:IO 線程數 | 1:1 | M:N(M>=N) | M:1 | M:0 |
阻塞類型 | 阻塞 | 阻塞 | 非阻塞 | 非阻塞 |
同步 | 同步 | 同步 | 同步(多路複用) | 異步 |
API 使用難度 | 簡單 | 簡單 | 複雜 | 通常 |
調試難度 | 簡單 | 簡單 | 複雜 | 複雜 |
可靠性 | 很是差 | 差 | 高 | 高 |
吞吐量 | 低 | 中 | 高 | 高 |