概述
JDK7引入了Asynchronous I/O。I/O编程中,经常使用到两种模式:Reactor 和 Proactor。Reactor就是Java的NIO。当有事件触发时,咱们获得通知,进行相应的处理。Proactor就是咱们今天要讲的 AIO了。AIO进行I/O操做,都是异步处理,当事件完成时,咱们会获得通知。
JDK7的 AIO包括网络和文件操做。二者大同小异,本文经过一个完整的客户端/服务器Sample来详细说明aio的网络操做。
AIO提供了两种异步操做的监听机制。第一种经过返回一个Future对象来事件,调用其get()会等到操做完成。第二种相似于回调函数。在进行异步操做时,传递一个CompletionHandler,当异步操做结束时,会调用CompletionHandler.complete 接口 html
范例
这个范例功能比较简单,就是客户端向服务端发送一个“test”命令,而后结束。
服务端程序 Sever.java
Java代码: java
import java.io.IOException; import java.net.InetSocketAddress; import java.nio.ByteBuffer; import java.nio.channels.AsynchronousServerSocketChannel; import java.nio.channels.AsynchronousSocketChannel; import java.util.concurrent.ExecutionException; import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; public class Server { private AsynchronousServerSocketChannel server; public Server()throws IOException{ server = AsynchronousServerSocketChannel.open().bind(new InetSocketAddress(8888)); } public void start() throws InterruptedException, ExecutionException, TimeoutException{ Future<AsynchronousSocketChannel> future = server.accept(); AsynchronousSocketChannel socket = future.get(); ByteBuffer readBuf = ByteBuffer.allocate(1024); socket.read(readBuf).get(100, TimeUnit.SECONDS); System.out.printf("Receiver:%s%n",new String(readBuf.array())); } public static void main(String args[]) throws Exception{ new Server().start(); } }客户端程序 (Future版本)
import java.io.IOException; import java.net.InetSocketAddress; import java.nio.ByteBuffer; import java.nio.channels.AsynchronousSocketChannel; import java.util.concurrent.ExecutionException; public class AIOClientWithFuture { private final AsynchronousSocketChannel client; public AIOClientWithFuture() throws IOException{ client = AsynchronousSocketChannel.open(); } public void sendMsg() throws InterruptedException, ExecutionException{ client.connect(new InetSocketAddress("localhost",8888)); client.write(ByteBuffer.wrap("test".getBytes())).get(); } public static void main(String...args) throws Exception{ AIOClientWithFuture client = new AIOClientWithFuture(); client.sendMsg(); } }客户端程序(CompleteHandler版本)
import java.net.InetSocketAddress; import java.nio.ByteBuffer; import java.nio.channels.AsynchronousSocketChannel; import java.nio.channels.CompletionHandler; public class AIOClientWithHandler { private final AsynchronousSocketChannel client ; public AIOClientWithHandler() throws Exception{ client = AsynchronousSocketChannel.open(); } public void start()throws Exception{ client.connect(new InetSocketAddress("127.0.0.1",8888),null,new CompletionHandler<Void,Void>() { @Override public void completed(Void result, Void attachment) { try { client.write(ByteBuffer.wrap("test".getBytes())).get(); } catch (Exception ex) { ex.printStackTrace(); } } @Override public void failed(Throwable exc, Void attachment) { exc.printStackTrace(); } }); } public static void main(String args[])throws Exception{ new AIOClientWithHandler().start(); } }
相关类说明:
AsynchronousSocketChannel 跟 SocketChannel操做相似,只不过改为异步接口了
AsynchronousServerSocketChannel跟ServerSocketChannel操做相似,只不过改为异步接口了
CompletionHandler 接口包括两个方法
void completed(V result, A attachment);
void failed(Throwable exc, A attachment); 编程
总结:
本文只是对jdk7的aio使用作了一个简单的说明。至于其性能提高多少,如何改进现有的网络应用程序,还在摸索中。这里推荐一个比较成熟的网络框架Project Grizzly:http://grizzly.dev.java.net。听说已经用aio从新实现了,有兴趣的同窗能够去研究一下源码。 服务器
参考资料:
(如下有些资料使用的jdk7版本过低,不少接口已经更改了,慎入!:) 网络
http://www.ibm.com/developerworks/java/library/j-nio2-1/index.html? 框架
http://www.iteye.com/topic/446298 异步
http://www.iteye.com/topic/472333 socket