NIO與IO讀檔案
阿新 • • 發佈:2019-02-15
對於讀檔案NIO與IO誰快?
如果加一些複雜環境,然後再讓他們去讀檔案呢?
為了得到這個答案,我做了兩組實驗,首先是直接單執行緒去讀一個很大的檔案,分別通過NIO和IO去讀:
/** * nio 讀 * * @param fileName */ public static void readFileWithNIO(String fileName) { try { long now = new Date().getTime(); FileInputStream fs = new FileInputStream(fileName); FileChannel fc = fs.getChannel(); ByteBuffer bb = ByteBuffer.allocate(1024); while (true) { bb.clear(); int count = fc.read(bb); if (count == -1) break; // System.out.println(new String(bb.array())); } fc.close(); fs.close(); System.out.println("nio read time:" + (new Date().getTime() - now) + "ms"); } catch (Exception e) { e.printStackTrace(); } }
public static void readFileWithIO(String fileName) { try { long now = new Date().getTime(); FileInputStream fs = new FileInputStream(fileName); byte bb[] = new byte[1024]; while (fs.read(bb) != -1) { // System.out.println(new String(bb)); } fs.close(); System.out.println("io read time:" + (new Date().getTime() - now) + "ms"); } catch (Exception e) { e.printStackTrace(); } }
結果相信測試過的人都知道,IO的速率一般來講都是NIO的3倍多,說明單執行緒讀檔案,IO比NIO快。
對於這個結論並不奇怪,因為NIO的特性明確標明是 非阻塞。單執行緒讀檔案,很難體現這個非阻塞的特點,非阻塞的言外之意是可以幹別的事情。於是,我做了如下實驗,我用多執行緒去讀檔案,也是分別用IO和NIO,這個時候的測試結果發現NIO和IO已經非常接近了,但是還是IO快。於是,我有增加了額外的執行緒在讀檔案的同時去幹一些其他的事情,這個時候,我終於發現NIO比IO快了,程式碼如下:
public static void readFileWithNIOMultThread(String fileName) { try { long now = new Date().getTime(); FileInputStream fs = new FileInputStream(fileName); final FileChannel fc = fs.getChannel(); class TestThread implements Runnable { public void run() { ByteBuffer bb = ByteBuffer.allocate(102400); try { while (true) { bb.clear(); int count = fc.read(bb); if (count == -1) break; // System.out.println(new String(bb.array())); // Thread.sleep((long)(Math.random() * 1000)); } } catch (Exception e) { e.printStackTrace(); } } } ArrayList<Thread> list = new ArrayList<Thread>(); for (int i = 0; i < 10; i++) { Thread tmp = new Thread(new TestThread()); list.add(tmp); tmp.start(); } for (int i = 0; i < 10; i++) { Thread tmp = new Thread(new Runnable() { public void run() { try { for(int i=0;i<1000;i++){ System.out.println(i); } } catch (Exception e) { e.printStackTrace(); } } }); list.add(tmp); tmp.start(); } for (Thread t : list) { t.join(); } fc.close(); fs.close(); System.out.println("nio read time:" + (new Date().getTime() - now) + "ms"); } catch (Exception e) { e.printStackTrace(); } }
public static void readFileWithIOMultThreads(String fileName) {
long now = new Date().getTime();
try {
final FileInputStream fs = new FileInputStream(fileName);
class TestThread implements Runnable {
public void run() {
byte bb[] = new byte[102400];
try {
while (fs.read(bb) != -1) {
// System.out.println(new String(bb));
// Thread.sleep((long)(Math.random() * 1000));
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
ArrayList<Thread> list = new ArrayList<Thread>();
for (int i = 0; i < 10; i++) {
Thread tmp = new Thread(new TestThread());
list.add(tmp);
tmp.start();
}
for (int i = 0; i < 10; i++) {
Thread tmp = new Thread(new Runnable() {
public void run() {
try {
for(int i=0;i<1000;i++){
System.out.println(i);
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
list.add(tmp);
tmp.start();
}
for (Thread t : list) {
t.join();
}
fs.close();
System.out.println("io read time:" + (new Date().getTime() - now)
+ "ms");
} catch (Exception e) {
e.printStackTrace();
}
}