欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java中BIO、NIO和AIO的區(qū)別、原理與用法

 更新時間:2021年12月15日 11:26:33   作者:卷卷之王  
這篇文章主要介紹了Java中BIO、NIO和AIO的區(qū)別、原理與用法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下

IO

什么是IO? 它是指計算機(jī)與外部世界或者一個程序與計算機(jī)的其余部分的之間的接口。它對于任何計算機(jī)系統(tǒng)都非常關(guān)鍵,因而所有 I/O 的主體實(shí)際上是內(nèi)置在操作系統(tǒng)中的。單獨(dú)的程序一般是讓系統(tǒng)為它們完成大部分的工作。

在 Java 編程中,直到最近一直使用 流 的方式完成 I/O。所有 I/O 都被視為單個的字節(jié)的移動,通過一個稱為 Stream 的對象一次移動一個字節(jié)。流 I/O 用于與外部世界接觸。它也在內(nèi)部使用,用于將對象轉(zhuǎn)換為字節(jié),然后再轉(zhuǎn)換回對象。

BIO

Java BIO即Block I/O , 同步并阻塞的IO。

BIO就是傳統(tǒng)的java.io包下面的代碼實(shí)現(xiàn)。

NIO

什么是NIO? NIO 與原來的 I/O 有同樣的作用和目的, 他們之間最重要的區(qū)別是數(shù)據(jù)打包和傳輸?shù)姆绞健T瓉淼?I/O 以流的方式處理數(shù)據(jù),而 NIO 以塊的方式處理數(shù)據(jù)。

面向流 的 I/O 系統(tǒng)一次一個字節(jié)地處理數(shù)據(jù)。一個輸入流產(chǎn)生一個字節(jié)的數(shù)據(jù),一個輸出流消費(fèi)一個字節(jié)的數(shù)據(jù)。為流式數(shù)據(jù)創(chuàng)建過濾器非常容易。鏈接幾個過濾器,以便每個過濾器只負(fù)責(zé)單個復(fù)雜處理機(jī)制的一部分,這樣也是相對簡單的。不利的一面是,面向流的 I/O 通常相當(dāng)慢。

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

各自適用場景

  • 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開始存儲
				 */
				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會隨著數(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);
 
  }
}

到此這篇關(guān)于Java中BIO、NIO和AIO的區(qū)別、原理與用法的文章就介紹到這了。希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • redis setIfAbsent和setnx的區(qū)別與使用說明

    redis setIfAbsent和setnx的區(qū)別與使用說明

    這篇文章主要介紹了redis setIfAbsent和setnx的區(qū)別與使用,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • Spring Boot實(shí)現(xiàn)數(shù)據(jù)訪問計數(shù)器方案詳解

    Spring Boot實(shí)現(xiàn)數(shù)據(jù)訪問計數(shù)器方案詳解

    在Spring Boot項(xiàng)目中,有時需要數(shù)據(jù)訪問計數(shù)器,怎么實(shí)現(xiàn)數(shù)據(jù)訪問計數(shù)器呢?下面小編給大家?guī)砹薙pring Boot數(shù)據(jù)訪問計數(shù)器的實(shí)現(xiàn)方案,需要的朋友參考下吧
    2021-08-08
  • Java線程在什么情況下可以終止

    Java線程在什么情況下可以終止

    Thread線程類自帶的stop方法,但是jdk不建議使用,因?yàn)閟top方法終止線程只是強(qiáng)行終止,內(nèi)存中部分值可能已發(fā)生變化,并未保證數(shù)據(jù)的一致性,將會導(dǎo)致線程安全問題,那么在什么情況下可以終止線程呢,本篇帶你探究一下
    2022-04-04
  • SpringBoot使用jasypt加解密密碼的實(shí)現(xiàn)方法(二)

    SpringBoot使用jasypt加解密密碼的實(shí)現(xiàn)方法(二)

    這篇文章主要介紹了SpringBoot使用jasypt加解密密碼的實(shí)現(xiàn)方法(二),本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-10-10
  • Java實(shí)現(xiàn)單鏈表基礎(chǔ)操作

    Java實(shí)現(xiàn)單鏈表基礎(chǔ)操作

    大家好,本篇文章主要講的是Java實(shí)現(xiàn)單鏈表基礎(chǔ)操作,感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下
    2022-02-02
  • 關(guān)于Kafka消息隊列原理的總結(jié)

    關(guān)于Kafka消息隊列原理的總結(jié)

    這篇文章主要介紹了關(guān)于Kafka消息隊列原理的總結(jié),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-05-05
  • Springboot通過谷歌Kaptcha?組件生成圖形驗(yàn)證碼功能

    Springboot通過谷歌Kaptcha?組件生成圖形驗(yàn)證碼功能

    Kaptcha是谷歌開源的一款簡單實(shí)用的圖形驗(yàn)證碼組件。我個人推薦它的最大原因是容易上手,采用約定大于配置的方式,快速契合到項(xiàng)目中,這篇文章主要介紹了Springboot通過谷歌Kaptcha組件生成圖形驗(yàn)證碼的方法,需要的朋友可以參考下
    2023-05-05
  • 背包問題-動態(tài)規(guī)劃java實(shí)現(xiàn)的分析與代碼

    背包問題-動態(tài)規(guī)劃java實(shí)現(xiàn)的分析與代碼

    這篇文章主要給大家介紹了關(guān)于背包問題動態(tài)規(guī)劃java實(shí)現(xiàn)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-12-12
  • SpringBoot?Security使用MySQL實(shí)現(xiàn)驗(yàn)證與權(quán)限管理

    SpringBoot?Security使用MySQL實(shí)現(xiàn)驗(yàn)證與權(quán)限管理

    安全管理是軟件系統(tǒng)必不可少的的功能。根據(jù)經(jīng)典的“墨菲定律”——凡是可能,總會發(fā)生。如果系統(tǒng)存在安全隱患,最終必然會出現(xiàn)問題,這篇文章主要介紹了SpringBoot安全管理Spring?Security基本配置
    2022-11-11
  • Spring實(shí)現(xiàn)動態(tài)修改時間參數(shù)并手動開啟關(guān)停操作

    Spring實(shí)現(xiàn)動態(tài)修改時間參數(shù)并手動開啟關(guān)停操作

    spring實(shí)現(xiàn)定時任務(wù)的方式有三種,分別是java自帶的timer類、spring task和quartz三種。本文只介紹spring自帶的task和第三方quartz,感興趣的朋友參考下吧
    2017-09-09

最新評論