Java core (a) in-depth understanding of BIO, NIO, AIO

  aims: 

 

  1. What is the difference BIO, NIO, AIO is?
  2. Synchronous / asynchronous, blocking / non-blocking what is the difference?
  3. What file read and write the most elegant implementation is?
  4. How to implement NIO multiplexing function?

 

A, IO introduction:

      (1) IO's full name is actually: abbreviation for Input / Output of. 

      (2) we usually refer to BIO is relative to the NIO is of BIO is to start at the beginning of the introduction of Java IO module operation, BIO is the acronym BlockingIO, by definition is to block IO mean.

1.1 difference BIO, NIO, AIO's

  • BIO is the traditional java.io package, which is based, interactive way to achieve synchronous flow model, blocking the way, that is to say when reading input stream or output stream before reading or writing operation is completed, the thread has been blocked where the linear sequence of the call therebetween reliable. It's a bit of code that is simple and intuitive; drawback is that the efficiency is very low IO and scalability, application performance easily become a bottleneck.
  • NIO java.nio package is introduced in Java 1.4, abstractions provide a new Channel, Selector, Buffer and the like, can be constructed multiplexed, synchronous non-blocking IO programs, while providing high performance closer to the underlying operating system of the data manipulation the way.
  •  AIO is introduced after 1.7 Java package, is an upgraded version of NIO provides asynchronous non-blocking IO mode of operation, so people call it AIO (Asynchronous IO), IO is based on an asynchronous event and a callback mechanism to achieve, that is, the application operation after return directly, there will not be clogged when the background processing is completed, the operating system will notify the appropriate thread for subsequent operations.

1.2 comprehensive understanding IO

     Traditional IO can be divided into four types:   

  • InputStream, OutputStream byte operations based IO
  • Writer, Reader character-based IO operations
  • File-based disk IO operations
  • Socket-based network operations IO

       note:

       (1) Scoket many times people java.net provided under it also classified as synchronous blocking IO, IO also because the network communication behavior.

       Classes and interfaces (2) java.io under many, but generally are InputStream, OutputStream, Writer, Reader subset of all master the use of these four classes and File is the key to good use of IO.

1.3 IO use

     Next Inheritance diagram InputStream, OutputStream, Writer, Reader, and use examples.

    FIG inheritance and class methods, as shown below:

          

 

InputStream Example of use:

InputStream inputStream = new FileInputStream("D:\\log.txt");
byte[] bytes = new byte[inputStream.available()];
inputStream.read(bytes);
String str = new String(bytes, "utf-8");
System.out.println(str);
inputStream.close();

1.3.2 OutputStream use

   

 

 

 OutputStream Example of use:

= The outputStream the OutputStream new new a FileOutputStream ( "D: \\ log.txt", to true ); // Parameter II indicates whether an additional, true = append 
outputStream.write ( "Hello, Pharaoh" .getBytes ( "utf-8 " )); 
outputStream.close ();

1.3.3 Writer use

  Writer and class inheritance diagram method, as shown below:   

      

 

 

 Writer use examples:

Writer = Writer new new FileWriter ( "D: \\ log.txt", to true ); // parameters Second, if additional documents, true = additional 
writer.append ( "Pharaoh, Hello" ); 
writer.Close () ;

1.3.4 Reader Use

  

 

 Reader Example of use:

Reader reader = new FileReader(filePath);
BufferedReader bufferedReader = new BufferedReader(reader);
StringBuffer bf = new StringBuffer();
String str;
while ((str = bufferedReader.readLine()) != null) {
    bf.append(str + "\n");
}
bufferedReader.close();
reader.close();
System.out.println(bf.toString());

二、同步、异步、阻塞、非阻塞

   2.1 同步与异步

      同步就是一个任务的完成需要依赖另外一个任务时,只有等待被依赖的任务完成后,依赖的任务才能算完成,这是一种可靠的任务序列。要么成功都成功,失败都失败,两个任务的状态可以保持一致。而异步是不需要等待被依赖的任务完成,只是通知被依赖的任务要完成什么工作,依赖的任务也立即执行,只要自己完成了整个任务就算完成了。至于被依赖的任务最终是否真正完成,依赖它的任务无法确定,所以它是不可靠的任务序列。我们可以用打电话和发短信来很好的比喻同步与异步操作。

  2.2 阻塞与非阻塞

      阻塞与非阻塞主要是从 CPU 的消耗上来说的,阻塞就是 CPU 停下来等待一个慢的操作完成 CPU 才接着完成其它的事。非阻塞就是在这个慢的操作在执行时 CPU 去干其它别的事,等这个慢的操作完成时,CPU 再接着完成后续的操作。虽然表面上看非阻塞的方式可以明显的提高 CPU 的利用率,但是也带了另外一种后果就是系统的线程切换增加。增加的 CPU 使用时间能不能补偿系统的切换成本需要好好评估。

      

参考博客: (1)详细理解:Java 的 I/O 类库的基本架构

                   (2)概述了解:Java 中 IO 流分为几种?字符流和字节流的区别?java处理流的优点?BIO,NIO,AIO 有什么区别?

 

Guess you like

Origin www.cnblogs.com/2019wxw/p/11674549.html