Understanding I / O model of Java NiO (I)

preface

I used to be weak in Java NiO, and I missed an opportunity because of this knowledge. So I'm going to study this part well recently. I think there will be friends like me who always want to understand this content. But I have no way to start. Every time I am asked about NiO, bio and AIO, I panic. Let's slowly understand NiO from some basic concepts.

Synchronous and asynchronous

Synchronous and asynchronous are easy to understand, and there are many explanations on the Internet. Below, I will explain these two concepts through personal understanding. It may be more popular. I hope it can be better understood.

Synchronization means that multiple tasks or events need to be executed one by one in order. If the tasks or events in front of the order are executed, the tasks or events in the back need to wait for the previous ones to be executed. These tasks or events cannot be executed concurrently. The synchronous execution task can be designed as a reliable task sequence, and the whole task is completed only when the two tasks can be consistent.

Asynchronous means that multiple tasks or events can be executed in parallel at the same time. The previous task will not lead to the waiting of the subsequent task. Because multiple tasks are carried out at the same time, there is no interdependence between each task, so reliability cannot be guaranteed.

Synchronization flowchart

Asynchronous flow chart

Synchronization sample code

For the method executed one by one in order, test3 will wait for both test1 and test2 to execute before executing.

Asynchronous sample code

As can be seen from the asynchronous code above, testa, testb and testc have their own threads to execute tasks, which are independent of each other, so there will be no task waiting. Typical asynchronous processing mechanism.

Although the above asynchrony is implemented with three threads, it does not mean that multithreading is asynchronous. These are two concepts. Multithreading is only a way to realize asynchrony. Asynchronous is a processing mode, which can be implemented in other ways in addition to multithreading.

In the example of life, when we make a phone call, it is equivalent to synchronization. Only when the other party is connected can the task be executed successfully. SMS sending is asynchronous, and it does not depend on whether the recipient receives the SMS successfully.

Blocking and non blocking

Blocking means that when a task is executing, it will issue a request operation. If the conditions required for the request operation are not met, it will wait until the conditions are met before continuing to perform other work.

Non blocking means that when a task is executing, it will issue a request operation. If the conditions required by the request operation are not met, it will immediately return a flag message to inform that the conditions are not met, instead of waiting all the time.

Blocking process

Non blocking process

Some people always confuse the concepts of synchronization and asynchrony with blocking and non blocking, but in fact, these are two completely different concepts.

The focus of the concepts of synchronization and asynchrony is whether the previous task will lead to the waiting of the whole process.

The focus of the concepts of blocking and non blocking is whether a flag message will be returned to inform if the operation request does not meet the condition.

In fact, the understanding of blocking and non blocking can be understood from the thread blocking we usually contact. When a slow task occurs, the thread will block, and the CPU will wait for the slow task to complete before executing the subsequent tasks. The non blocking thread will do other things when executing this slow task. When the slow task is completed, it will execute the following tasks. Although it seems that non blocking can significantly improve the efficiency, the thread switching of the system will also cause time loss, so it needs to be used reasonably.

Synchronous IO and asynchronous IO

Synchronous IO means that when a thread is performing IO operations, the thread will be blocked before the IO operations are completed.

Asynchronous IO means that when a thread is performing IO operations, the thread will not be blocked.

IO operation actually has a process. Let's take network IO as an example. A network IO mainly involves two objects, one is the thread calling the IO, and the other is the system kernel. When a read operation occurs, it goes through two stages.

1. Wait for the data to be ready.

2. Copy the data from the kernel to the thread calling this io.

The difference between IO models is mainly in these two stages, so it is very important. The difference between synchronous and asynchronous lies in the second stage, Copy the data from the kernel to the thread (or process). If it is blocked, it will be synchronized. If it is not blocked, it is asynchronous. If it is blocked, it means that the operation at this stage depends on the user thread, and if it is not blocked, it means that it does not depend on the user thread, but on the kernel. Therefore, asynchrony needs to be supported by the operating system kernel.

Blocking IO and non blocking IO

When we introduced synchronous IO and asynchronous IO, we said that the difference between synchronous and asynchronous is in the second stage of IO operation. In this section, blocked IO and non blocked IO occur in the first stage of IO operation.

Blocking IO means that when a thread initiates an IO operation request, the system kernel will check whether the data to be operated is ready. When blocking IO, if it is found that the data to be operated is not ready, it will wait until the data is ready; When it is non blocking IO, if the data is not ready, an identification information will be returned to tell the calling thread that the current operation data is not ready. The first phase will not be performed until the data is ready.

In fact, the key difference between blocking IO and non blocking IO is whether to wait for execution or return a notification ID immediately. When the data is not ready, it waits for execution. When a notification ID is returned immediately, the thread will know what the data is now according to the ID. if it is not ready, the thread will initiate a request again and execute immediately after knowing that the data is ready.

Combination of two methods

Although asynchrony and non blocking can improve the performance of I / O, it will also bring some additional performance costs. For example, it will increase the number of threads, increase the consumption of CPU, and increase the complexity of program design. If the design is unreasonable, it will lead to performance degradation. In the actual design, it is necessary to decompose the application scenario for comprehensive evaluation.

The following table lists the performance analysis of the combination of synchronous asynchronous and blocking non blocking.

The article will be synchronized to my official account.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>