这些天我正在学习JavaNIO2.API,我做了一个测试来比较AIO和标准IO之间的效率,测试是编写一个2000米的file.Here作为代码。
标准的方法:
FileOutputStream output = new FileOutputStream(tmp);
byte[] byteArr = new byte[1024 * 1024];
for (int i = 0; i < 1024 * 1024; i++) {
byteArr[i] = 1;
}
long start = System.currentTimeMillis();
while (length -- > 0) {
output.write(byteArr);
}
System.out.println("taking time:" + (System.currentTimeMillis() - start) + "ms");结果是taking time:10392ms
AIO方式:
AsynchronousFileChannel afc = AsynchronousFileChannel.open(path, WRITE, CREATE);
List<Future<Integer>> results = new ArrayList<>();
ByteBuffer buf = ByteBuffer.allocate(1024 * 1024);
buf.clear();
for (int j = 0; j < 1024 * 1024; j++) {
buf.put((byte) 1);
}
buf.flip();
buf.mark();
long start = System.currentTimeMillis();
for (int i = 0; i < 2000; i ++) {
buf.reset();
results.add(afc.write(buf, i * 1024 *1024));
}
for(Future<Integer> future : results) {
future.get();
}
System.out.println("taking time:" + (System.currentTimeMillis() - start) + "ms");结果是taking time:15652ms
我认为,在第二种情况下,程序向操作系统提交了2000份书面请求。操作系统将使用自己的线程池运行IO操作。换句话说,thread pool size IO操作将同时执行。在第二种情况下,它应该更快。但事实正好相反,为什么?
发布于 2018-03-16 02:32:10
在程序提交写入请求之前,文件被锁定,直到写入operation.In (另一个词)结束,AsynchronousFileChannel只允许在同一个time.So多线程上执行一个操作无效。
https://stackoverflow.com/questions/49251486
复制相似问题