Java IO mode

Original please indicate the source: https://www.cnblogs.com/agilestyle/p/11444349.html

 

BIO

The traditional java.io package, which is based on flow model implementation, IO offers some of our most well-known features, such as File abstract, input and output streams. Interaction is synchronous, blocking the way, that is to say, while reading the input stream or the output stream, in reading, writing before the action is completed, the thread has been blocked there, calls between them is reliable linear order.

Benefits java.io package is code is simple, intuitive, the disadvantage is that IO efficiency and scalability limitations, application performance tends to be a bottleneck.

Many times, people also put part of the network API provides the following java.net, such as Socket, ServerSocket, HttpURLConnection also classified into synchronous blocking IO library, because network traffic is also IO behavior.

 

NIO

Introduced in the Java 1.4 NIO frame (the java.nio package) provides a new abstract Channel, Selector, Buffer and the like, can be constructed multiplexed, synchronous non-blocking IO programs, while providing the underlying operating system nearer high performance data operation.

The main part of the NIO

  • Buffer, efficient data container, in addition to the Boolean type, all primitive has a corresponding Buffer achieved.

  • Channel, similar to a file descriptor on the class of the Linux operating system to see is the NIO was used to support an abstract batch IO operations.

    File or Socket, it usually considered to be relatively high level of abstraction, while Channel is a more abstract the underlying operating system, which also makes full use of modern NIO to mechanisms underlying operating system, optimized for performance of a particular scene, e.g., the DMA (Direct Memory Access) and so on. Different levels of abstraction are interrelated, we can get Channel by Socket, and vice versa.

  • Selector, NIO be multiplexed is based, which provides an efficient mechanism to detect a plurality of registered on Channel Selector, the Channel whether there is in a ready state, so as to realize a single-thread multi-Channel efficient management.

  • Chartset, providing Unicode strings defined, NIO also provides a corresponding codecs

Selector which is also based on the underlying operating system mechanisms, different models, different versions there are differences, for example, in the latest code library, related to achieve the following:

Linux dependent on epoll ( http://hg.openjdk.java.net/jdk/jdk/file/d8327f838b88/src/java.base/linux/classes/sun/nio/ch/EPollSelectorImpl.java )

NIO is to use a single-threaded polling event mechanism, by efficiently locate ready Channel, to decide what to do, select the stage just blocked, can effectively avoid a large number of client connections, problems caused by frequent thread switching, scalable applications with very large increase.

 

AIO

In Java 7, NIO has been further improved, which is NIO 2, the introduction of asynchronous non-blocking IO mode, there are a lot of people call it AIO (Asynchronous IO). Event-based asynchronous IO operation and callback mechanism, can be simply understood as the application operating direct return, without blocking there when background processing is complete, the operating system will notify the appropriate follow up the thread.

 

Reference

https://time.geekbang.org/column/article/8369

 

Guess you like

Origin www.cnblogs.com/agilestyle/p/11444349.html