Java I/O 操作及优化建议

Java I/O

I/O。即 Input/Output(输入/输出) 的简称。

就 I/O 而言。概念上有 5 种模型:blocking I/O,nonblocking I/O。I/O multiplexing (select and poll),signal driven I/O (SIGIO)。asynchronous I/O (the POSIX aio_functions)。不同的操作系统对上述模型支持不同,UNIX 支持 IO 多路复用。不同系统叫法不同。freebsd 里面叫 kqueue,Linux 叫 epoll。而 Windows2000 的时候就诞生了 IOCP 用以支持 asynchronous I/O。

Java 是一种跨平台语言。为了支持异步 I/O,诞生了 NIO,Java1.4 引入的 NIO1.0 是基于 I/O 复用的,它在各个平台上会选择不同的复用方式。Linux 用的 epoll,BSD 上用 kqueue,Windows 上是重叠 I/O。

Java I/O 的相关方法例如以下所述:

  • 同步并堵塞 (I/O 方法):server实现模式为一个连接启动一个线程,每一个线程亲自处理 I/O 而且一直等待 I/O 直到完毕,即客户端有连接请求时server端就须要启动一个线程进行处理。可是假设这个连接不做不论什么事情就会造成不必要的线程开销。当然能够通过线程池机制改善这个缺点。I/O 的局限是它是面向流的、堵塞式的、串行的一个过程。对每一个客户端的 Socket 连接 I/O 都须要一个线程来处理,而且在此期间,这个线程一直被占用。直到 Socket 关闭。在这期间。TCP 的连接、数据的读取、数据的返回都是被堵塞的。也就是说这期间大量浪费了 CPU 的时间片和线程占用的内存资源。此外。每建立一个 Socket 连接时。同一时候创建一个新线程对该 Socket 进行单独通信 (採用堵塞的方式通信)。

    这样的方式具有非常快的响应速度,而且控制起来也非常easy。

    在连接数较少的时候非常有效。可是假设对每一个连接都产生一个线程无疑是对系统资源的一种浪费,假设连接数较多将会出现资源不足的情况;

  • 同步非堵塞 (NIO 方法):server实现模式为一个请求启动一个线程。每一个线程亲自处理 I/O,可是另外的线程轮询检查是否 I/O 准备完毕。不必等待 I/O 完毕,即客户端发送的连接请求都会注冊到多路复用器上,多路复用器轮询到连接有 I/O 请求时才启动一个线程进行处理。NIO 则是面向缓冲区,非堵塞式的。基于选择器的,用一个线程来轮询监控多个传输数据通道,哪个通道准备好了 (即有一组能够处理的数据) 就处理哪个通道。server端保存一个 Socket 连接列表,然后对这个列表进行轮询。假设发现某个 Socket 端口上有数据可读时,则调用该 Socket 连接的对应读操作;假设发现某个 Socket 端口上有数据可写时。则调用该 Socket 连接的对应写操作。假设某个端口的 Socket 连接已经中断,则调用对应的析构方法关闭该端口。

    这样能充分利用server资源,效率得到大幅度提高;

  • 异步非堵塞 (AIO 方法,JDK7 公布):server实现模式为一个有效请求启动一个线程,客户端的 I/O 请求都是由操作系统先完毕了再通知server应用去启动线程进行处理。每一个线程不必亲自处理 I/O,而是委派操作系统来处理,而且也不须要等待 I/O 完毕,假设完毕了操作系统会另行通知的。该模式採用了 Linux 的 epoll 模型。

在连接数不多的情况下,传统 I/O 模式编写较为easy。使用上也较为简单。

可是随着连接数的不断增多,传统 I/O 处理每一个连接都须要消耗一个线程。而程序的效率,当线程数不多时是随着线程数的添加而添加,可是到一定的数量之后。是随着线程数的添加而降低的。所以传统堵塞式 I/O 的瓶颈在于不能处理过多的连接。非堵塞式 I/O 出现的目的就是为了解决这个瓶颈。

非堵塞 IO 处理连接的线程数和连接数没有联系。比如系统处理 10000 个连接,非堵塞 I/O 不须要启动 10000 个线程,你能够用 1000 个,也能够用 2000 个线程来处理。因为非堵塞 IO 处理连接是异步的,当某个连接发送请求到server,server把这个连接请求当作一个请求“事件”。并把这个“事件”分配给对应的函数处理。我们能够把这个处理函数放到线程中去执行,执行完就把线程归还,这样一个线程就能够异步的处理多个事件。而堵塞式 I/O 的线程的大部分时间都被浪费在等待请求上了。

Java NIO

Java.nio 包是 Java 在 1.4 版本号之后新添加的包。专门用来提高 I/O 操作的效率。

表 1 所看到的是 I/O 与 NIO 之间的对照内容。

表 1. I/O VS NIO
I/ONIO
面向流面向缓冲
堵塞 IO非堵塞 IO
选择器

NIO 是基于块 (Block) 的,它以块为基本单位处理数据。

在 NIO 中,最为重要的两个组件是缓冲 Buffer 和通道 Channel。缓冲是一块连续的内存块。是 NIO 读写数据的中转地。

通道标识缓冲数据的源头或者目的地,它用于向缓冲读取或者写入数据,是訪问缓冲的接口。Channel 是一个双向通道,就可以读,也可写。Stream 是单向的。应用程序不能直接对 Channel 进行读写操作,而必须通过 Buffer 来进行。即 Channel 是通过 Buffer 来读写数据的。

使用 Buffer 读写数据一般遵循下面四个步骤:

  1. 写入数据到 Buffer。
  2. 调用 flip() 方法。
  3. 从 Buffer 中读取数据。
  4. 调用 clear() 方法或者 compact() 方法。

当向 Buffer 写入数据时,Buffer 会记录下写了多少数据。一旦要读取数据,须要通过 flip() 方法将 Buffer 从写模式切换到读模式。在读模式下。能够读取之前写入到 Buffer 的全部数据。

一旦读完了全部的数据,就须要清空缓冲区。让它能够再次被写入。有两种方式能清空缓冲区:调用 clear() 或 compact() 方法。clear() 方法会清空整个缓冲区。compact() 方法仅仅会清除已经读过的数据。不论什么未读的数据都被移到缓冲区的起始处。新写入的数据将放到缓冲区未读数据的后面。

Buffer 有多种类型,不同的 Buffer 提供不同的方式操作 Buffer 中的数据。

图 1 Buffer 接口层次图

图 1 Buffer 接口层次图

Buffer 写数据有两种情况:

  1. 从 Channel 写到 Buffer,如样例中 Channel 从文件里读取数据,写到 Channel;
  2. 直接调用 put 方法,往里面写数据。

从 Buffer 中读取数据有两种方式:

  1. 从 Buffer 读取数据到 Channel;
  2. 使用 get() 方法从 Buffer 中读取数据。

Buffer 的 rewin 方法将 position 设回 0,所以你能够重读 Buffer 中的全部数据。

limit 保持不变,仍然表示能从 Buffer 中读取多少个元素(byte、char 等)。

clear() 和 compact() 方法

一旦读完 Buffer 中的数据,须要让 Buffer 准备好再次被写入。能够通过 clear() 或 compact() 方法来完毕。

假设调用的是 clear() 方法,position 将被设回 0,limit 被设置成 capacity 的值。

换句话说。Buffer 被清空了。Buffer 中的数据并未清除。仅仅是这些标记告诉我们能够从哪里開始往 Buffer 里写数据。

假设 Buffer 中有一些未读的数据,调用 clear() 方法。数据将“被遗忘”,意味着不再有不论什么标记会告诉你哪些数据被读过,哪些还没有。假设 Buffer 中仍有未读的数据,且兴许还须要这些数据,可是此时想要先写些数据,那么使用 compact() 方法。

compact() 方法将全部未读的数据复制到 Buffer 起始处。然后将 position 设到最后一个未读元素正后面。

limit 属性依旧像 clear() 方法一样,设置成 capacity。如今 Buffer 准备好写数据了,可是不会覆盖未读的数据。

Buffer 參数

Buffer 有 3 个重要的參数:位置 (position)、容量 (capacity) 和上限 (limit)。

capacity 是指 Buffer 的大小。在 Buffer 建立的时候已经确定。

limit 当 Buffer 处于写模式,指还能够写入多少数据。处于读模式,指还有多少数据能够读。

position 当 Buffer 处于写模式,指下一个写数据的位置;处于读模式,当前将要读取的数据的位置。

每读写一个数据,position+1,也就是 limit 和 position 在 Buffer 的读/写时的含义不一样。

当调用 Buffer 的 flip 方法。由写模式变为读模式时,limit(读)=position(写),position(读) =0。

散射&聚集

NIO 提供了处理结构化数据的方法,称之为散射 (Scattering) 和聚集 (Gathering)。散射是指将数据读入一组 Buffer 中,而不仅仅是一个。

聚集与之相反,指将数据写入一组 Buffer 中。散射和聚集的基本用法和对单个 Buffer 操作时的用法相当相似。在散射读取中,通道依次填充每一个缓冲区。填满一个缓冲区后,它就開始填充下一个,在某种意义上,缓冲区数组就像一个大缓冲区。在已知文件详细结构的情况下,能够构造若干个符合文件结构的 Buffer,使得各个 Buffer 的大小恰好符合文件各段结构的大小。此时。通过散射读的方式能够一次将内容装配到各个对应的 Buffer 中。从而简化操作。

假设须要创建指定格式的文件,仅仅要先构造好大小合适的 Buffer 对象。使用聚集写的方式,便能够非常快地创建出文件。清单 1 以 FileChannel 为例。展示怎样使用散射和聚集读写结构化文件。

清单 1. 使用散射和聚集读写结构化文件
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
 
public class NIOScatteringandGathering {
 public void createFiles(String TPATH){
 try {
 ByteBuffer bookBuf = ByteBuffer.wrap("java 性能优化技巧".getBytes("utf-8"));
ByteBuffer autBuf = ByteBuffer.wrap("test".getBytes("utf-8"));
int booklen = bookBuf.limit();
int autlen = autBuf.limit();
ByteBuffer[] bufs = new ByteBuffer[]{bookBuf,autBuf};
File file = new File(TPATH);
if(!file.exists()){
try {
file.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try {
FileOutputStream fos = new FileOutputStream(file);
FileChannel fc = fos.getChannel();
fc.write(bufs);
fos.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
 
ByteBuffer b1 = ByteBuffer.allocate(booklen);
ByteBuffer b2 = ByteBuffer.allocate(autlen);
ByteBuffer[] bufs1 = new ByteBuffer[]{b1,b2};
File file1 = new File(TPATH);
try {
FileInputStream fis = new FileInputStream(file);
FileChannel fc = fis.getChannel();
fc.read(bufs1);
String bookname = new String(bufs1[0].array(),"utf-8");
String autname = new String(bufs1[1].array(),"utf-8");
System.out.println(bookname+" "+autname);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
 
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
 
 }
 
 public static void main(String[] args){
 NIOScatteringandGathering nio = new NIOScatteringandGathering();
 nio.createFiles("C:\1.TXT");
 }
}


 

输出例如以下清单 2 所看到的。

清单 2. 执行结果
1
java 性能优化技巧 test

清单 3 所看到的代码对传统 I/O、基于 Byte 的 NIO、基于内存映射的 NIO 三种方式进行了性能上的对照,使用一个有 400 万数据的文件的读、写操作耗时作为评測根据。

清单 3. I/O 的三种方式对照试验
import java.io.BufferedInputStream;

import java.io.BufferedOutputStream;

import java.io.DataInputStream;

import java.io.DataOutputStream;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.RandomAccessFile;

import java.nio.ByteBuffer;

import java.nio.IntBuffer;

import java.nio.MappedByteBuffer;

import java.nio.channels.FileChannel;

 

public class NIOComparator {

 public void IOMethod(String TPATH){

 long start = System.currentTimeMillis();

 try {

DataOutputStream dos = new DataOutputStream(

 new BufferedOutputStream(new FileOutputStream(new File(TPATH))));

for(int i=0;i<4000000;i++){

dos.writeInt(i);//写入 4000000 个整数

}

if(dos!=null){

dos.close();

}

 } catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

 } catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

 }

 long end = System.currentTimeMillis();

 System.out.println(end - start);

 start = System.currentTimeMillis();

 try {

DataInputStream dis = new DataInputStream(

 new BufferedInputStream(new FileInputStream(new File(TPATH))));

for(int i=0;i<4000000;i++){

dis.readInt();

}

if(dis!=null){

dis.close();

}

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

 

 end = System.currentTimeMillis();

 System.out.println(end - start);

 }

 

 public void ByteMethod(String TPATH){

 long start = System.currentTimeMillis();

 try {

FileOutputStream fout = new FileOutputStream(new File(TPATH));

FileChannel fc = fout.getChannel();//得到文件通道

ByteBuffer byteBuffer = ByteBuffer.allocate(4000000*4);//分配 Buffer

for(int i=0;i<4000000;i++){

byteBuffer.put(int2byte(i));//将整数转为数组

}

byteBuffer.flip();//准备写

fc.write(byteBuffer);

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

 long end = System.currentTimeMillis();

 System.out.println(end - start);

 

 start = System.currentTimeMillis();

 FileInputStream fin;

try {

fin = new FileInputStream(new File(TPATH));

FileChannel fc = fin.getChannel();//取得文件通道

ByteBuffer byteBuffer = ByteBuffer.allocate(4000000*4);//分配 Buffer

fc.read(byteBuffer);//读取文件数据

fc.close();

byteBuffer.flip();//准备读取数据

while(byteBuffer.hasRemaining()){

byte2int(byteBuffer.get(),byteBuffer.get(),byteBuffer.get(),byteBuffer.get());//将 byte 转为整数

}

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

 end = System.currentTimeMillis();

 System.out.println(end - start);

 }

 

 public void mapMethod(String TPATH){

 long start = System.currentTimeMillis();

 //将文件直接映射到内存的方法

 try {

FileChannel fc = new RandomAccessFile(TPATH,"rw").getChannel();

IntBuffer ib = fc.map(FileChannel.MapMode.READ_WRITE, 0, 4000000*4).asIntBuffer();

for(int i=0;i<4000000;i++){

ib.put(i);

}

if(fc!=null){

fc.close();

}

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

 long end = System.currentTimeMillis();

 System.out.println(end - start);

 

 start = System.currentTimeMillis();

 try {

FileChannel fc = new FileInputStream(TPATH).getChannel();

MappedByteBuffer lib = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());

lib.asIntBuffer();

while(lib.hasRemaining()){

lib.get();

}

if(fc!=null){

fc.close();

}

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

 end = System.currentTimeMillis();

 System.out.println(end - start);

 

 }

 

 public static byte[] int2byte(int res){

 byte[] targets = new byte[4];

 targets[3] = (byte)(res & 0xff);//最低位

 targets[2] = (byte)((res>>8)&0xff);//次低位

 targets[1] = (byte)((res>>16)&0xff);//次高位

 targets[0] = (byte)((res>>>24));//最高位,无符号右移

 return targets;

 }

 

 public static int byte2int(byte b1,byte b2,byte b3,byte b4){

 return ((b1 & 0xff)<<24)|((b2 & 0xff)<<16)|((b3 & 0xff)<<8)|(b4 & 0xff);

 }

 

 public static void main(String[] args){

 NIOComparator nio = new NIOComparator();

 nio.IOMethod("c:\1.txt");

 nio.ByteMethod("c:\2.txt");

 nio.ByteMethod("c:\3.txt");

 }

}

清单 3 执行输出如清单 4 所看到的。

清单 4. 执行输出
 
 
 
 
 
 
1139
906
296
157
234
125

除上述描写叙述及清单 3 所看到的代码以外,NIO 的 Buffer 还提供了一个能够直接訪问系统物理内存的类 DirectBuffer。DirectBuffer 继承自 ByteBuffer,但和普通的 ByteBuffer 不同。普通的 ByteBuffer 仍然在 JVM 堆上分配空间。其最大内存受到最大堆的限制。而 DirectBuffer 直接分配在物理内存上,并不占用堆空间。

在对普通的 ByteBuffer 訪问时,系统总是会使用一个“内核缓冲区”进行间接的操作。

而 DirectrBuffer 所处的位置,相当于这个“内核缓冲区”。因此,使用 DirectBuffer 是一种更加接近系统底层的方法,所以。它的速度比普通的 ByteBuffer 更快。

DirectBuffer 相对于 ByteBuffer 而言。读写訪问速度快非常多,可是创建和销毁 DirectrBuffer 的花费却比 ByteBuffer 高。DirectBuffer 与 ByteBuffer 相比較的代码如清单 5 所看到的。

清单 5. DirectBuffer VS ByteBuffer
import java.nio.ByteBuffer;

 

public class DirectBuffervsByteBuffer {

 public void DirectBufferPerform(){

 long start = System.currentTimeMillis();

 ByteBuffer bb = ByteBuffer.allocateDirect(500);//分配 DirectBuffer

 for(int i=0;i<100000;i++){

 for(int j=0;j<99;j++){

 bb.putInt(j);

 }

 bb.flip();

 for(int j=0;j<99;j++){

 bb.getInt(j);

 }

 }

 bb.clear();

 long end = System.currentTimeMillis();

 System.out.println(end-start);

 start = System.currentTimeMillis();

 for(int i=0;i<20000;i++){

 ByteBuffer b = ByteBuffer.allocateDirect(10000);//创建 DirectBuffer

 }

 end = System.currentTimeMillis();

 System.out.println(end-start);

 }

 

 public void ByteBufferPerform(){

 long start = System.currentTimeMillis();

 ByteBuffer bb = ByteBuffer.allocate(500);//分配 DirectBuffer

 for(int i=0;i<100000;i++){

 for(int j=0;j<99;j++){

 bb.putInt(j);

 }

 bb.flip();

 for(int j=0;j<99;j++){

 bb.getInt(j);

 }

 }

 bb.clear();

 long end = System.currentTimeMillis();

 System.out.println(end-start);

 start = System.currentTimeMillis();

 for(int i=0;i<20000;i++){

 ByteBuffer b = ByteBuffer.allocate(10000);//创建 ByteBuffer

 }

 end = System.currentTimeMillis();

 System.out.println(end-start);

 }

 

 public static void main(String[] args){

 DirectBuffervsByteBuffer db = new DirectBuffervsByteBuffer();

 db.ByteBufferPerform();

 db.DirectBufferPerform();

 }

}

执行输出如清单 6 所看到的。

清单 6. 执行输出
 
 
 
 
920
110
531
390

由清单 6 可知,频繁创建和销毁 DirectBuffer 的代价远远大于在堆上分配内存空间。使用參数-XX:MaxDirectMemorySize=200M –Xmx200M 在 VM Arguments 里面配置最大 DirectBuffer 和最大堆空间,代码中分别请求了 200M 的空间。假设设置的堆空间过小,比如设置 1M。会抛出错误如清单 7 所看到的。

清单 7. 执行错误
 
 
Error occurred during initialization of VM
Too small initial heap for new size specified

DirectBuffer 的信息不会打印在 GC 里面,因为 GC 仅仅记录了堆空间的内存回收。能够看到。因为 ByteBuffer 在堆上分配空间,因此其 GC 数组相对非常频繁,在须要频繁创建 Buffer 的场合,因为创建和销毁 DirectBuffer 的代码比較高昂。不宜使用 DirectBuffer。可是假设能将 DirectBuffer 进行复用,能够大幅改善系统性能。清单 8 是一段对 DirectBuffer 进行监控代码。

清单 8. 对 DirectBuffer 监控代码
import java.lang.reflect.Field;
 
public class monDirectBuffer {
 
public static void main(String[] args){
try {
Class c = Class.forName("java.nio.Bits");//通过反射取得私有数据
Field maxMemory = c.getDeclaredField("maxMemory");
maxMemory.setAccessible(true);
Field reservedMemory = c.getDeclaredField("reservedMemory");
reservedMemory.setAccessible(true);
synchronized(c){
Long maxMemoryValue = (Long)maxMemory.get(null);
Long reservedMemoryValue = (Long)reservedMemory.get(null);
System.out.println("maxMemoryValue="+maxMemoryValue);
System.out.println("reservedMemoryValue="+reservedMemoryValue);
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchFieldException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
 
}
}



 

清单 9. 执行输出
1
2
maxMemoryValue=67108864
reservedMemoryValue=0

因为 NIO 使用起来较为困难,所以很多公司推出了自己封装 JDK NIO 的框架,比如 Apache 的 Mina,JBoss 的 Netty。Sun 的 Grizzly 等等,这些框架都直接封装了传输层的 TCP 或 UDP 协议,当中 Netty 仅仅是一个 NIO 框架,它不须要 Web 容器的额外支持,也就是说不限定 Web 容器。

Java AIO

AIO 相关的类和接口:

  • java.nio.channels.AsynchronousChannel:标记一个 Channel 支持异步 IO 操作;
  • java.nio.channels.AsynchronousServerSocketChannel:ServerSocket 的 AIO 版本号,创建 TCP 服务端。绑定地址,监听端口等;
  • java.nio.channels.AsynchronousSocketChannel:面向流的异步 Socket Channel。表示一个连接;
  • java.nio.channels.AsynchronousChannelGroup:异步 Channel 的分组管理,目的是为了资源共享。一个 AsynchronousChannelGroup 绑定一个线程池。这个线程池执行两个任务:处理 IO 事件和派发 CompletionHandler。AsynchronousServerSocketChannel 创建的时候能够传入一个 AsynchronousChannelGroup。那么通过 AsynchronousServerSocketChannel 创建的 AsynchronousSocketChannel 将同属于一个组,共享资源;
  • java.nio.channels.CompletionHandler:异步 IO 操作结果的回调接口,用于定义在 IO 操作完毕后所作的回调工作。AIO 的 API 同意两种方式来处理异步操作的结果:返回的 Future 模式或者注冊 CompletionHandler。推荐用 CompletionHandler 的方式。这些 handler 的调用是由 AsynchronousChannelGroup 的线程池派发的。这里线程池的大小是性能的关键因素。

这里举一个程序范例,简介一下 AIO 怎样运作。

清单 10. 服务端程序
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.AsynchronousServerSocketChannel;
import java.nio.channels.AsynchronousSocketChannel;
import java.nio.channels.CompletionHandler;
import java.util.concurrent.ExecutionException;
 
public class SimpleServer {
public SimpleServer(int port) throws IOException { 
final AsynchronousServerSocketChannel listener = 
 AsynchronousServerSocketChannel.open().bind(new InetSocketAddress(port));
//监听消息,收到后启动 Handle 处理模块
listener.accept(null, new CompletionHandler<AsynchronousSocketChannel, Void>() {
public void completed(AsynchronousSocketChannel ch, Void att) { 
listener.accept(null, this);// 接受下一个连接 
handle(ch);// 处理当前连接 
}
 
@Override
public void failed(Throwable exc, Void attachment) {
// TODO Auto-generated method stub
 
} 
 
});
}
 
public void handle(AsynchronousSocketChannel ch) { 
ByteBuffer byteBuffer = ByteBuffer.allocate(32);//开一个 Buffer 
try { 
 ch.read(byteBuffer).get();//读取输入 
} catch (InterruptedException e) { 
 // TODO Auto-generated catch block 
 e.printStackTrace(); 
} catch (ExecutionException e) { 
 // TODO Auto-generated catch block 
 e.printStackTrace(); 
} 
byteBuffer.flip(); 
System.out.println(byteBuffer.get()); 
// Do something 
} 
 
}


清单 11. 客户端程序

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.AsynchronousSocketChannel;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
 
public class SimpleClientClass {
private AsynchronousSocketChannel client; 
public SimpleClientClass(String host, int port) throws IOException, 
                                    InterruptedException, ExecutionException { 
 this.client = AsynchronousSocketChannel.open(); 
 Future<?> future = client.connect(new InetSocketAddress(host, port)); 
 future.get(); 
} 
 
public void write(byte b) { 
 ByteBuffer byteBuffer = ByteBuffer.allocate(32);
 System.out.println("byteBuffer="+byteBuffer);
 byteBuffer.put(b);//向 buffer 写入读取到的字符 
 byteBuffer.flip();
 System.out.println("byteBuffer="+byteBuffer);
 client.write(byteBuffer); 
} 
 
}


清单 12.Main 函数

import java.io.IOException;
import java.util.concurrent.ExecutionException;
 
import org.junit.Test;
 
public class AIODemoTest {
 
@Test
public void testServer() throws IOException, InterruptedException { 
 SimpleServer server = new SimpleServer(9021); 
 Thread.sleep(10000);//因为是异步操作,所以睡眠一定时间,以免程序非常快结束
} 
 
@Test
public void testClient() throws IOException, InterruptedException, ExecutionException { 
SimpleClientClass client = new SimpleClientClass("localhost", 9021); 
 client.write((byte) 11); 
}
 
public static void main(String[] args){
AIODemoTest demoTest = new AIODemoTest();
try {
demoTest.testServer();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
demoTest.testClient();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
 
}

兴许会专门出文章详细深入介绍 AIO 的源码、设计理念、设计模式等等。

结束语

I/O 与 NIO 一个比較重要的差别是我们使用 I/O 的时候往往会引入多线程,每一个连接使用一个单独的线程。而 NIO 则是使用单线程或者仅仅使用少量的多线程。每一个连接共用一个线程。

而因为 NIO 的非堵塞须要一直轮询,比較消耗系统资源,所以异步非堵塞模式 AIO 就诞生了。

本文对 I/O、NIO、AIO 等三种输入输出操作方式进行一一介绍,力求通过简单的描写叙述和实例让读者能够掌握主要的操作、优化方法。

本文原文地址:http://www.importnew.com/16481.html


 

原文地址:https://www.cnblogs.com/liguangsunls/p/7040513.html