BIO、NIO和AIO的區(qū)別、三種IO的原理與用法

IO

什么是IO? 它是指計(jì)算機(jī)與外部世界或者一個(gè)程序與計(jì)算機(jī)的其余部分的之間的接口。它對于任何計(jì)算機(jī)系統(tǒng)都非常關(guān)鍵,因而所有 I/O 的主體實(shí)際上是內(nèi)置在操作系統(tǒng)中的。單獨(dú)的程序一般是讓系統(tǒng)為它們完成大部分的工作。
在 Java 編程中,直到最近一直使用 流 的方式完成 I/O。所有 I/O 都被視為單個(gè)的字節(jié)的移動(dòng),通過一個(gè)稱為 Stream 的對象一次移動(dòng)一個(gè)字節(jié)。流 I/O 用于與外部世界接觸。它也在內(nèi)部使用,用于將對象轉(zhuǎn)換為字節(jié),然后再轉(zhuǎn)換回對象。
網(wǎng)絡(luò)IO磁盤IO

BIO

Java BIO即Block I/O , 同步并阻塞的IO。
BIO就是傳統(tǒng)的java.io包下面的代碼實(shí)現(xiàn)。

NIO

什么是NIO? NIO 與原來的 I/O 有同樣的作用和目的, 他們之間最重要的區(qū)別是數(shù)據(jù)打包和傳輸?shù)姆绞?。原來?I/O 以的方式處理數(shù)據(jù),而 NIO 以的方式處理數(shù)據(jù)。
面向流 的 I/O 系統(tǒng)一次一個(gè)字節(jié)地處理數(shù)據(jù)。一個(gè)輸入流產(chǎn)生一個(gè)字節(jié)的數(shù)據(jù),一個(gè)輸出流消費(fèi)一個(gè)字節(jié)的數(shù)據(jù)。為流式數(shù)據(jù)創(chuàng)建過濾器非常容易。鏈接幾個(gè)過濾器,以便每個(gè)過濾器只負(fù)責(zé)單個(gè)復(fù)雜處理機(jī)制的一部分,這樣也是相對簡單的。不利的一面是,面向流的 I/O 通常相當(dāng)慢
一個(gè) 面向塊 的 I/O 系統(tǒng)以塊的形式處理數(shù)據(jù)。每一個(gè)操作都在一步中產(chǎn)生或者消費(fèi)一個(gè)數(shù)據(jù)塊。按塊處理數(shù)據(jù)比按(流式的)字節(jié)處理數(shù)據(jù)要得多。但是面向塊的 I/O 缺少一些面向流的 I/O 所具有的優(yōu)雅性和簡單性。

AIO

Java AIO即Async非阻塞,是異步非阻塞的IO。

區(qū)別及聯(lián)系

  • BIO (Blocking I/O):同步阻塞I/O模式,數(shù)據(jù)的讀取寫入必須阻塞在一個(gè)線程內(nèi)等待其完成。這里假設(shè)一個(gè)燒開水的場景,有一排水壺在燒開水,BIO的工作模式就是, 叫一個(gè)線程停留在一個(gè)水壺那,直到這個(gè)水壺?zé)_,才去處理下一個(gè)水壺。但是實(shí)際上線程在等待水壺?zé)_的時(shí)間段什么都沒有做。
  • NIO (New I/O)(not blocking i/o):同時(shí)支持阻塞與非阻塞模式,但這里我們以其同步非阻塞I/O模式來說明,那么什么叫做同步非阻塞?如果還拿燒開水來說,NIO的做法是叫一個(gè)線程不斷的輪詢每個(gè)水壺的狀態(tài),看看是否有水壺的狀態(tài)發(fā)生了改變,從而進(jìn)行下一步的操作。
  • AIO ( Asynchronous I/O):異步非阻塞I/O模型。異步非阻塞與同步非阻塞的區(qū)別在哪里?異步非阻塞無需一個(gè)線程去輪詢所有IO操作的狀態(tài)改變,在相應(yīng)的狀態(tài)改變后,系統(tǒng)會(huì)通知對應(yīng)的線程來處理。對應(yīng)到燒開水中就是,為每個(gè)水壺上面裝了一個(gè)開關(guān),水燒開之后,水壺會(huì)自動(dòng)通知我水燒開了。

各自適用場景

  • BIO方式適用于連接數(shù)目比較小且固定的架構(gòu),這種方式對服務(wù)器資源要求比較高,并發(fā)局限于應(yīng)用中,JDK1.4以前的唯一選擇,但程序直觀簡單易理解。
  • NIO方式適用于連接數(shù)目多且連接比較短(輕操作)的架構(gòu),比如聊天服務(wù)器,并發(fā)局限于應(yīng)用中,編程比較復(fù)雜,JDK1.4開始支持。
  • AIO方式適用于連接數(shù)目多且連接比較長(重操作)的架構(gòu),比如相冊服務(wù)器,充分調(diào)用OS參與并發(fā)操作,編程比較復(fù)雜,JDK7開始支持。

使用方式

使用BIO實(shí)現(xiàn)文件的讀取和寫入。

 
       //Initializes The Object
        User1 user = new User1();
        user.setName("hollis");
        user.setAge(23);
        System.out.println(user);
 
        //Write Obj to File
        ObjectOutputStream oos = null;
        try {
            oos = new ObjectOutputStream(new FileOutputStream("tempFile"));
            oos.writeObject(user);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            IOUtils.closeQuietly(oos);
        }
 
        //Read Obj from File
        File file = new File("tempFile");
        ObjectInputStream ois = null;
        try {
            ois = new ObjectInputStream(new FileInputStream(file));
            User1 newUser = (User1) ois.readObject();
            System.out.println(newUser);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            IOUtils.closeQuietly(ois);
            try {
                FileUtils.forceDelete(file);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
 
       //Initializes The Object
        User1 user = new User1();
        user.setName("hollis");
        user.setAge(23);
        System.out.println(user);
 
        //Write Obj to File
        ObjectOutputStream oos = null;
        try {
            oos = new ObjectOutputStream(new FileOutputStream("tempFile"));
            oos.writeObject(user);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            IOUtils.closeQuietly(oos);
        }
 
        //Read Obj from File
        File file = new File("tempFile");
        ObjectInputStream ois = null;
        try {
            ois = new ObjectInputStream(new FileInputStream(file));
            User1 newUser = (User1) ois.readObject();
            System.out.println(newUser);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            IOUtils.closeQuietly(ois);
            try {
                FileUtils.forceDelete(file);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

使用NIO實(shí)現(xiàn)文件的讀取和寫入

 
static void readNIO() {
        String pathname = "C:\\Users\\adew\\Desktop\\jd-gui.cfg";
        FileInputStream fin = null;
        try {
            fin = new FileInputStream(new File(pathname));
            FileChannel channel = fin.getChannel();
 
            int capacity = 100;// 字節(jié)
            ByteBuffer bf = ByteBuffer.allocate(capacity);
            System.out.println("限制是:" + bf.limit() + "容量是:" + bf.capacity()
                    + "位置是:" + bf.position());
            int length = -1;
 
            while ((length = channel.read(bf)) != -1) {
 
                /*
                 * 注意,讀取后,將位置置為0,將limit置為容量, 以備下次讀入到字節(jié)緩沖中,從0開始存儲(chǔ)
                 */
                bf.clear();
                byte[] bytes = bf.array();
                System.out.write(bytes, 0, length);
                System.out.println();
 
                System.out.println("限制是:" + bf.limit() + "容量是:" + bf.capacity()
                        + "位置是:" + bf.position());
 
            }
 
            channel.close();
 
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fin != null) {
                try {
                    fin.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
 
    static void writeNIO() {
        String filename = "out.txt";
        FileOutputStream fos = null;
        try {
 
            fos = new FileOutputStream(new File(filename));
            FileChannel channel = fos.getChannel();
            ByteBuffer src = Charset.forName("utf8").encode("你好你好你好你好你好");
            // 字節(jié)緩沖的容量和limit會(huì)隨著數(shù)據(jù)長度變化,不是固定不變的
            System.out.println("初始化容量和limit:" + src.capacity() + ","
                    + src.limit());
            int length = 0;
 
            while ((length = channel.write(src)) != 0) {
                /*
                 * 注意,這里不需要clear,將緩沖中的數(shù)據(jù)寫入到通道中后 第二次接著上一次的順序往下讀
                 */
                System.out.println("寫入長度:" + length);
            }
 
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
 

使用AIO實(shí)現(xiàn)文件的讀取和寫入

public class ReadFromFile {
  public static void main(String[] args) throws Exception {
    Path file = Paths.get("/usr/a.txt");
    AsynchronousFileChannel channel = AsynchronousFileChannel.open(file);
 
    ByteBuffer buffer = ByteBuffer.allocate(100_000);
    Future<Integer> result = channel.read(buffer, 0);
 
    while (!result.isDone()) {
      ProfitCalculator.calculateTax();
    }
    Integer bytesRead = result.get();
    System.out.println("Bytes read [" + bytesRead + "]");
  }
}
class ProfitCalculator {
  public ProfitCalculator() {
  }
  public static void calculateTax() {
  }
}
 
public class WriteToFile {
 
  public static void main(String[] args) throws Exception {
    AsynchronousFileChannel fileChannel = AsynchronousFileChannel.open(
        Paths.get("/asynchronous.txt"), StandardOpenOption.READ,
        StandardOpenOption.WRITE, StandardOpenOption.CREATE);
    CompletionHandler<Integer, Object> handler = new CompletionHandler<Integer, Object>() {
 
      @Override
      public void completed(Integer result, Object attachment) {
        System.out.println("Attachment: " + attachment + " " + result
            + " bytes written");
        System.out.println("CompletionHandler Thread ID: "
            + Thread.currentThread().getId());
      }
 
      @Override
      public void failed(Throwable e, Object attachment) {
        System.err.println("Attachment: " + attachment + " failed with:");
        e.printStackTrace();
      }
    };
 
    System.out.println("Main Thread ID: " + Thread.currentThread().getId());
    fileChannel.write(ByteBuffer.wrap("Sample".getBytes()), 0, "First Write",
        handler);
    fileChannel.write(ByteBuffer.wrap("Box".getBytes()), 0, "Second Write",
        handler);
 
  }
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容