This is the 20th day of my participation in the August Text Challenge.More challenges in August

| author: jiangxia

| CSDN:blog.csdn.net/qq_41153943

| the nuggets: juejin. Cn/user / 651387…

| zhihu: www.zhihu.com/people/1024…

| GitHub:github.com/JiangXia-10…

This article is about 3,045 words and takes 9 minutes to read

1, concept,

BIO, NIO and AIO in Java are the encapsulation of various IO models of the operating system in Java language. Programmers use these apis without the need to care about operating system knowledge or write different code for different operating systems. Just use the Java API.

Before we get to BIO,NIO, and AIO, let’s review a few concepts: synchronous versus asynchronous, blocking versus non-blocking. And they can be arranged and combined: synchronous blocking, synchronous non-blocking, asynchronous blocking, and asynchronous non-blocking.

1. Synchronization: a user process triggers an I/O operation and waits or polls to check whether the I/O operation is ready. For example, hungry go to a restaurant to have a meal, after ordering good food, ask the boss all the time, the meal is good to have no.

2. Asynchronous: Asynchronous means that the user process can do its own work after triggering THE IO operation. When the IO operation is completed, it will be notified of the COMPLETION of the IO. When asynchronous I/O is used, Java delegates I/O reads and writes to the OS and sends the ADDRESS and size of the data buffer to the OS.

There are three ways to notify the caller, as follows:

Status: that is to listen to the status of the called (polling), the caller needs to check once every certain time, the efficiency will be very low.

Notification: Notifies the caller when the caller is done without consuming too much performance.

Callback: Similar to notification, a callback function provided by the caller is called when the caller completes execution.

In summary, the difference between synchronous and asynchronous is whether, after a request is issued, you need to wait for the result before you can proceed with other operations.

3. Block: The so-called blocking mode means that when attempting to read or write to the file descriptor, if there is nothing to read at that time, or temporarily unwritable, the program will enter a waiting state until there is something to read or write. For example, when we go to eat, the meal is not ready, then we are in a state of congestion.

4. Non-blocking: In the non-blocking state, if there is nothing to read or write to, the read/write function returns immediately instead of waiting forever. For example, when we have dinner, if the meal does not come, we can play games, watch TV, and wait for the meal, instead of waiting for the meal.

So: 1. Synchronous and asynchronous are for application/kernel interaction. 2, blocking and non-blocking is aimed at the process at the time of access to data, according to the ready state of IO operations to take different ways, in plain English is a read or write operation function is implemented, blocking the way of read and write functions will have been waiting for, rather than blocking way, read and write functions would immediately return a status value. That is, synchronous and asynchronous are the goals, while blocking and non-blocking are the implementations ****.

An IO operation actually consists of two steps: making an I/O request and the actual I/O operation **. 支那

The difference between synchronous I/O and asynchronous I/O is whether the actual I/O operation blocks. If the actual I/O operation blocks the request process, it is synchronous I/O.

Synchronous and asynchronous interaction is in view of the application and the kernel, synchronization refers to the user process triggered IO operation and wait for or polling to see IO operations readiness, and asynchronous refers to the user process triggered IO operations later began to do his own thing, and when the IO operation has been completed will get IO completion notification.

The difference between blocking I/O and non-blocking I/O is whether the request to initiate the I/O will be blocked. If the request is blocked until it is completed, it will be blocked. If it is not blocked, it will be non-blocking I/O.

Blocking and non-blocking is aimed at the process at the time of access to data, according to the ready state of IO operations to take different ways, in plain English is a read or write operation function is implemented, blocking the way of read and write functions will have been waiting for, rather than blocking way, read and write functions would immediately return a status value.

Therefore, IO operations can be divided into three categories: BIO(Blocked IO), NIO(non-blocked IO), and AIO(Asynchronous IO).

Java BIO (Blocking IO) : Synchronization and blocking. Before JDK1.4 came out, we used BIO mode to set up a network connection. We started a ServerSocket on the server, and then started the Socket on the client to communicate with the server. After sending a request, the client first consults the server to see if there is a thread corresponding to the request. If there is no thread corresponding to the request, the client will wait or reject the request. If there is a thread corresponding to the request, the client will wait for the request to end before continuing to execute. So there is unnecessary thread overhead if the connection does nothing, but this can be remedied by thread pooling.

Java NIO (non-blocking IO) : Synchronous non-blocking, the server implementation mode is one request one thread, that is, the connection request sent by the client is registered with the multiplexer, the multiplexer polls the connection to have I/O requests to start a thread for processing.

Java AIO(NIO.2) (Asynchronous IO) : Asynchronous, non-blocking, server implementation mode is one valid request to one thread, client I/O requests are completed by the OS and then notify the server application to start the thread to process.

There is also asynchronous blocking IO, which is more called IO Multiplexing than the classic Reactor design pattern. Multiplexing is the use of a single thread to check the ready state of multiple file descriptors (sockets), such as calling select and poll functions, passing in multiple file descriptors, returning if one of them is ready, otherwise blocking until timeout. The actual operation performed after the ready state is achieved can be performed in the same thread, or it can be initiated (for example, using a thread pool). In this way, when dealing with too many connections, only 1 thread is needed to monitor the ready state, and only one thread is needed to handle each connection. In this way, the number of threads required is greatly reduced, reducing the memory overhead and CPU overhead of context switch.

Here’s an example:

When we buy snacks in the night market, we often have such scenes: for example, we order stinky tofu at the stinky tofu stall, but there are too many people, so we have to queue up for our turn to get stinky tofu, which is synchronous blocking; But sometimes we can at this point a stinky tofu tofu, and then talk to the boss to buy a Fried rice with egg will pick it, but don’t know when will it be good, after all the stinky tofu hot delicious, so when we were waiting for the Fried rice with egg back in a few minutes to go and see a good stinky tofu, Fried rice in the stands and stinky tofu stand is synchronous non-blocking run back and forth; Another is that we have been the old customers of stinky tofu stall told the boss to a stinky tofu, first to buy eggs fried rice, stinky tofu good call and then go to take, this is asynchronous blocking; Another situation is that you are too friendly with the boss. The boss says you can buy fried rice with eggs and I will send it to you when stinky tofu is ready. This is asynchronous non-blocking.

2. Application scenarios

BIO, NIO, AND AIO application scenario analysis:

BIO mode is suitable for small and fixed number of connections. This mode requires high server resources, concurrency is limited to the application, and the only choice before JDK1.4, but the program is intuitive, simple and easy to understand.

NIO is suitable for architectures with a large number of connections and relatively short (light operation) connections, such as chat servers, where concurrency is limited to applications and programming is complicated. JDK1.4 supports NIO.

AIO mode is used in the architecture with a large number of connections and long connections (heavy operation), such as photo album server. It fully calls OS to participate in concurrent operations, and the programming is complicated, which is supported by JDK7.

3, the end

In this paper, BIO, NIO and AIO in Java are simply explained and analyzed from the concept and application layer. In fact, the principle behind this paper involves many underlying concepts, such as network and operating system, etc. There is no need to expand the explanation too much because of the limited space here.

Related recommendations:

  • Spring annotation (2) : @ComponentScan Automatically scans components
  • Spring is worth your collection!!
  • Spring annotation (7) : Assign attributes to beans using @value
  • SpringBoot develops Restful interfaces to implement CRUD functions
  • Introduction to distributed cache middleware Redis