java1.4提供的NIO已经很厉害了,大神们再接再厉,在java1.7又提出了新的NIO2.0——AIO
AIO是真正的异步非阻塞IO,Linux底层依赖epoll,我们先看个案例
//服务端 public class AIOServer { private static int port =8080; public static void main(String[] args) { AsyncServerHandler asyncServerHandler = new AsyncServerHandler(port); new Thread(asyncServerHandler).start(); } } // package aio; import java.io.IOException; import java.net.InetSocketAddress; import java.nio.channels.AsynchronousChannel; import java.nio.channels.AsynchronousServerSocketChannel; import java.util.concurrent.CountDownLatch; public class AsyncServerHandler implements Runnable{ public CountDownLatch latch; public AsynchronousServerSocketChannel channel; public AsyncServerHandler(int port) { try { channel=AsynchronousServerSocketChannel.open(); channel.bind(new InetSocketAddress(port)); System.out.println("服务端已启动"); } catch (IOException e) { e.printStackTrace(); } } @Override public void run() { latch=new CountDownLatch(1); channel.accept(this,new AcceptHandler()); try { latch.await(); } catch (InterruptedException e) { e.printStackTrace(); } } } // package aio; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.nio.ByteBuffer; import java.nio.channels.AsynchronousSocketChannel; import java.nio.channels.CompletionHandler; public class ReadeHandler implements CompletionHandler<Integer, ByteBuffer> { private AsynchronousSocketChannel channel; public ReadeHandler(AsynchronousSocketChannel channel) { this.channel = channel; } @Override public void completed(Integer result, ByteBuffer attachment) { attachment.flip(); byte[] bytes = new byte[attachment.remaining()]; attachment.get(bytes); try { String s = new String(bytes, "UTF-8"); System.out.println("server receive:" + s); doWrite("hello," + s); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } private void doWrite(String result) { byte[] bytes = result.getBytes(); ByteBuffer writeBuffer = ByteBuffer.allocate(bytes.length); writeBuffer.put(bytes); writeBuffer.flip(); channel.write(writeBuffer, writeBuffer, new WriteHandler(channel)); } @Override public void failed(Throwable exc, ByteBuffer attachment) { try { this.channel.close(); } catch (IOException e) { e.printStackTrace(); } } } // package aio; import java.io.IOException; import java.nio.ByteBuffer; import java.nio.channels.AsynchronousSocketChannel; import java.nio.channels.CompletionHandler; public class WriteHandler implements CompletionHandler<Integer, ByteBuffer> { private AsynchronousSocketChannel channel; public WriteHandler(AsynchronousSocketChannel channel) { this.channel = channel; } @Override public void completed(Integer result, ByteBuffer buffer) { if (buffer.hasRemaining()) { channel.write(buffer, buffer, this); } else { ByteBuffer readBuffer = ByteBuffer.allocate(1024); channel.read(readBuffer, readBuffer, new ReadeHandler(channel)); } } @Override public void failed(Throwable exc, ByteBuffer attachment) { try { this.channel.close(); } catch (IOException e) { e.printStackTrace(); } } }
package aio; import java.nio.ByteBuffer; import java.nio.channels.AsynchronousSocketChannel; import java.nio.channels.CompletionHandler; public class AcceptHandler implements CompletionHandler<AsynchronousSocketChannel, AsyncServerHandler> { @Override public void completed(AsynchronousSocketChannel channel, AsyncServerHandler serverHandler) { serverHandler.channel.accept(serverHandler, this); ByteBuffer buffer = ByteBuffer.allocate(1024); channel.read(buffer, buffer, new ReadeHandler(channel)); } @Override public void failed(Throwable exc, AsyncServerHandler serverHandler) { exc.printStackTrace(); serverHandler.latch.countDown(); } }
客户端代码
package aio; import java.util.Scanner; public class AIOClient { private static int port = 8080; private static String host = "127.0.0.1"; public static void main(String[] args) { AsyncClientHandler asyncClientHandler = new AsyncClientHandler(host, port); new Thread(asyncClientHandler).start(); Scanner scanner = new Scanner(System.in); while (scanner.hasNextLine()){ String msg = scanner.nextLine(); asyncClientHandler.sendMsg(msg); } } } // package aio; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.nio.ByteBuffer; import java.nio.channels.AsynchronousSocketChannel; import java.nio.channels.CompletionHandler; import java.util.concurrent.CountDownLatch; public class ClientReadeHandler implements CompletionHandler<Integer, ByteBuffer> { private AsynchronousSocketChannel channel; private CountDownLatch latch; public ClientReadeHandler(AsynchronousSocketChannel channel,CountDownLatch latch) { this.channel = channel; this.latch=latch; } @Override public void completed(Integer result, ByteBuffer attachment) { attachment.flip(); byte[] bytes = new byte[attachment.remaining()]; attachment.get(bytes); try { String s = new String(bytes, "UTF-8"); System.out.println("client receive:" + s); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } @Override public void failed(Throwable exc, ByteBuffer attachment) { System.out.println("数据读取失败"); try { this.channel.close(); this.latch.countDown(); } catch (IOException e) { e.printStackTrace(); } } } // package aio; import java.io.IOException; import java.nio.ByteBuffer; import java.nio.channels.AsynchronousSocketChannel; import java.nio.channels.CompletionHandler; import java.util.concurrent.CountDownLatch; public class ClientWriteHandler implements CompletionHandler<Integer, ByteBuffer> { private AsynchronousSocketChannel channel; private CountDownLatch latch; public ClientWriteHandler(AsynchronousSocketChannel channel,CountDownLatch latch) { this.channel = channel; this.latch=latch; } @Override public void completed(Integer result, ByteBuffer buffer) { if (buffer.hasRemaining()) { channel.write(buffer, buffer, this); } else { ByteBuffer readBuffer = ByteBuffer.allocate(1024); channel.read(readBuffer, readBuffer, new ClientReadeHandler(channel,latch)); } } @Override public void failed(Throwable exc, ByteBuffer attachment) { try { this.channel.close(); latch.countDown(); } catch (IOException e) { e.printStackTrace(); } } }
标签:java,nio,编程,AIO,ByteBuffer,import,public,channel From: https://www.cnblogs.com/wangbin2188/p/17179062.html