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的支持主要提供如下的一些封装类:
[b]AsynchronousChannel[/b]:所有AIO Channel的父类。
[b]AsynchronousByteChannel[/b]:支持Byte读写的Channel
[b]AsynchronousDatagramChannel[/b]:支持数据包(datagram)读写的Channel
[b]AsynchronousFileChannel[/b]:支持文件读写的Channel
[b]AsynchronousServerSocketChannel[/b]:支持数据流读写的服务器端Channel
[b]AsynchronousSocketChannel[/b]:支持数据流读写的客户端Channel
[b]AsynchronousChannelGroup[/b]:支持资源共享的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();
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值