首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网络技术 > 网络基础 >

JDK7 AIO 入门(1)-AsynchronousFileChannel

2012-08-26 
JDK7 AIO 入门(一)------AsynchronousFileChannel随着JDK7的发布,Java的AIO正式支持版本也出炉了,就像当年

JDK7 AIO 入门(一)------AsynchronousFileChannel
   随着JDK7的发布,Java的AIO正式支持版本也出炉了,就像当年发布NIO特性支持时,基本上所有的Java服务器都重写了自己的网络框架以通过NIO来提高服务器的性能。AIO的发布势必也会引起Java界的一次重写风潮,现在很多的网络框架(如Mina),大型软件(如Oracle DB)都宣布自己已经在新版本中支持了AIO的特性以提高性能。下面就来看一下aio的基本原理,以及如何使用JDK7的AIO特性。
    所谓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();    }}

热点排行