所謂AIO,異步IO,其主要是針對進程在調用IO獲取外部數據時,是否阻塞調用進程而言的。一個進程的IO調用步驟大致如下:
1、進程向操作系統請求數據
2、操作系統把外部數據加載到內核的緩衝區中,
3、操作系統把內核的緩衝區拷貝到進程的緩衝區
4、進程獲得數據完成自己的功能
當操作系統在把外部數據放到進程緩衝區的這段時間(即上述的第二,三步),如果應用進程是掛起等待的,那麼就是同步IO,反之,就是異步IO,也就是AIO
JDK對於IO支持基本上都是基於操作系統的封裝,而IO操作的核心就是如何有效的管理Channel(數據通道),JDK7對AIO的支持主要提供如下的一些封裝類:
AsynchronousChannel:所有AIO Channel的父類。
AsynchronousByteChannel:支持Byte讀寫的Channel
AsynchronousDatagramChannel:支持數據包(datagram)讀寫的Channel
AsynchronousFileChannel:支持文件讀寫的Channel
AsynchronousServerSocketChannel:支持數據流讀寫的服務器端Channel
AsynchronousSocketChannel:支持數據流讀寫的客戶端Channel
AsynchronousChannelGroup:支持資源共享的Channel分組
對於AIO的Channel,JDK定義了2種類型的操作,
1、Future operation(....):即 通過Future判斷是操作是否完成。
2、void operation(Object attachment, CompletionHandler handler):即通過CompletionHandler來通知異步IO完成。
下面就是一個AsynchronousFileChannel實現的完整異步讀寫文件的例子
- package com.jdk7.io.aio;
- import java.io.IOException;
- import java.nio.ByteBuffer;
- import java.nio.channels.AsynchronousFileChannel;
- import java.nio.channels.CompletionHandler;
- import java.nio.file.Path;
- import java.nio.file.Paths;
- import java.util.concurrent.ExecutionException;
- import java.util.concurrent.Future;
- /**
- * Created with IntelliJ IDEA.
- * User: twer
- * Date: 5/2/12
- * Time: 8:37 PM
- * To change this template use File | Settings | File Templates.
- */
- public class AFCDemo {
- static Thread current;
- public static void main(String[] args) throws IOException, InterruptedException, ExecutionException {
- if (args == null || args.length == 0) {
- System.out.println("Please input file path");
- return;
- }
- Path filePath = Paths.get(args[0]);
- AsynchronousFileChannel afc = AsynchronousFileChannel.open(filePath);
- ByteBuffer byteBuffer = ByteBuffer.allocate(16 * 1024);
- //使用FutureDemo時,請註釋掉completionHandlerDemo,反之亦然
- futureDemo(afc, byteBuffer);
- completionHandlerDemo(afc, byteBuffer);
- }
- private static void completionHandlerDemo(AsynchronousFileChannel afc, ByteBuffer byteBuffer) throws IOException {
- current = Thread.currentThread();
- afc.read(byteBuffer, 0, null, new CompletionHandler<Integer, Object>() {
- @Override
- public void completed(Integer result, Object attachment) {
- System.out.println("Bytes Read = " + result);
- current.interrupt();
- }
- @Override
- public void failed(Throwable exc, Object attachment) {
- System.out.println(exc.getCause());
- current.interrupt();
- }
- });
- System.out.println("Waiting for completion...");
- try {
- current.join();
- } catch (InterruptedException e) {
- }
- System.out.println("End");
- afc.close();
- }
- private static void futureDemo(AsynchronousFileChannel afc, ByteBuffer byteBuffer) throws InterruptedException, ExecutionException, IOException {
- Future<Integer> result = afc.read(byteBuffer, 0);
- while (!result.isDone()) {
- System.out.println("Waiting file channel finished....");
- Thread.sleep(1);
- }
- System.out.println("Finished? = " + result.isDone());
- System.out.println("byteBuffer = " + result.get());
- System.out.println(byteBuffer);
- afc.close();
- }
- }