Java 异步IO(1) 文件异步IO
2014-11-13 19:35
375 查看
大家写过多线程都用过java.util.concurrent包,自己创建线程池,自己实现run 或者call接口创建线程类,之后提交给线程池运行。
之前大家读写文件,必须得等待IO完成。如果要想节约时间,还得自己实现上面多线程那一套。今天刚知道异步IO这个东西,不用自己实现多线程了。下面是示例代码:
之前大家读写文件,必须得等待IO完成。如果要想节约时间,还得自己实现上面多线程那一套。今天刚知道异步IO这个东西,不用自己实现多线程了。下面是示例代码:
public static void asynchronousFileIO() { Path path = Paths.get("resource/Stopword.txt"); try { AsynchronousFileChannel channel = AsynchronousFileChannel.open(path); ByteBuffer buffer = ByteBuffer.allocate(1000); Future<Integer> future = channel.read(buffer, 0); while (!future.isDone()) { System.out.println("Do others..."); } Integer number = future.get(); System.out.println("Read " + number + " content : " + new String(buffer.array())); channel.close(); } catch (IOException | InterruptedException | ExecutionException e) { e.printStackTrace(); } }
相关文章推荐
- Java 异步IO(2) 文件异步IO Callback
- 异步IO、APC、IO完成端口、线程池与高性能服务器之一 异步IO
- java IO 系统-java 文件操作大全
- java读取文件的两种方法:java.io和java.lang.ClassLoader
- 同步文件IO和异步文件IO
- 转载:异步IO、APC、IO完成端口、线程池与高性能服务器之一 异步IO
- java连接oracle,取io文件内容,子串替换(原作)!
- java io对文件的操作
- java关于(io&nio) 的 文件copy例子
- 典型文件操作 (java.io.*)
- Java中各种文件类型操作的代码与详细文件IO讲解
- 用java的i/o读写文件,举一反三看清io的使用 推荐
- 关于异步文件IO
- 关于异步文件IO
- java读取文件的两种方法:java.io和java.lang.ClassLoader
- 使用java.io.*操作文件的拆分与合并,Application一例!
- 使用java.io.*操作文件的拆分与合并,Application一例!
- 使用java.io.*操作文件的拆分与合并,Application一例!
- Java IO 实现文件复制 -Java 学习笔记 (25)
- 使用java.io.*操作文件的拆分与合并,Application一例