Understanding of bio, NiO and AIO in Java
In the design of high-performance IO system, several terms and concepts often confuse us. The details are as follows:
1 what is synchronization? 2 what is asynchronous? 3 what is blocking? 4 what is non blocking? 5 what is synchronous blocking? 6 what is synchronous non blocking? 7 what is asynchronous blocking? 8 what is asynchronous non blocking?
Let's take an example from life:
If you want to eat a kung pao chicken covered rice: synchronous blocking: you go to the restaurant to order, and then wait there. You have to shout: OK! Synchronous non blocking: after ordering in the restaurant, I went to walk the dog. But slip away for a while, then go back to the restaurant and shout: OK, no! Asynchronous blocking: when walking the dog, I received a call from the restaurant saying that the meal is ready and let you pick it up in person. Asynchronous non blocking: the restaurant called and said that we know your location. We'll send it to you later and walk the dog at ease.
Before clarifying the above problems, we must first understand what is synchronous, asynchronous, blocking and non blocking. Only these single concepts are understood clearly, and then it is relatively easy to understand them in combination.
1. Synchronization and asynchrony are aimed at the interaction between the application and the kernel.
2. Blocking and non blocking are different methods adopted by the process according to the ready state of IO operation when accessing data. In short, it is an implementation method of read or write operation function. In blocking mode, the read or write function will wait all the time, while in non blocking mode, the read or write function will immediately return a status value.
From the above description, we can basically summarize a short sentence. Synchronization and asynchrony are the purpose, and blocking and non blocking are the implementation methods.
1. Synchronization: refers to that the user process triggers the IO operation and waits or polls to check whether the IO operation is ready. Buy clothes on the street and do it yourself. I can't do anything else.
2. Asynchrony: asynchrony means that the user process starts to do its own things after triggering the IO operation, When the IO operation has been completed, you will be notified of the completion of IO (the asynchronous feature is notification) and tell your friends the size, size and color of the appropriate clothes, so that your friends can entrust you to sell them, and then you can do other things. (when using asynchronous IO, Java entrusts IO read and write to the OS for processing, and you need to pass the data buffer address and size to the OS.)
3. Blocking: the so-called blocking means that when trying to read and write the file descriptor, if there is nothing to read or write at that time, the program will enter the waiting state until there is something to read or write. Go to the bus station to recharge and find it at this time, The recharger is not there (maybe he went to the bathroom), and then we wait here until the recharger comes back. (of course, in real society, this is not the case, but it is true in computers.)
4. Non blocking: in the non blocking state, if there is nothing to read or write, the read-write function will return immediately without waiting. When withdrawing money from the bank for business, we will get a small ticket. After receiving it, we can play with our mobile phone or chat with others. When we turn, the bank's horn will notify us, and then we can go.
An IO operation is actually divided into two steps: initiating an IO request and the actual IO operation.
The difference between synchronous IO and asynchronous IO lies in whether the second step is blocked. If the actual IO read / write blocks the requesting process, it is synchronous io.
The difference between blocking IO and non blocking IO lies in the first step: whether the originating IO request will be blocked. If it is blocked until it is completed, it is a traditional blocking io. If it is not blocked, it is a non blocking io.
Synchronization and asynchrony refer to the interaction between the application and the kernel. Synchronization refers to that the user process triggers the IO operation and waits or polls to check whether the IO operation is ready. Asynchrony refers to that the user process starts to do its own things after triggering the IO operation, and will be notified of IO completion when the IO operation has been completed.
Blocking and non blocking are different methods adopted by the process according to the ready state of IO operation when accessing data. In short, it is an implementation method of read or write operation function. In blocking mode, the read or write function will always wait, while in non blocking mode, the read or write function will immediately return a status value.
Therefore, IO operations can be divided into three categories: synchronous blocking (i.e. early bio operations), synchronous non blocking (NiO) and asynchronous non blocking (AIO).
Synchronization blocking (bio):
In this way, after initiating an IO operation, the user process must wait for the completion of the IO operation. The user process can run only after the IO operation is truly completed. Java traditional IO model belongs to this way.
Synchronous non blocking (NiO):
In this way, the user process can return to do other things after initiating an IO operation, but the user process needs to ask whether the IO operation is ready from time to time, which requires the user process to keep asking, thus introducing unnecessary waste of CPU resources. At present, NiO of Java belongs to synchronous non blocking io.
Asynchronous non blocking (AIO):
In this mode, after an application initiates an IO operation, it does not wait for the completion of the kernel IO operation. After the kernel completes the IO operation, it will notify the application.
Synchronous blocking IO (Java bio):
Synchronization and blocking. The server implementation mode is one connection and one thread. That is, when the client has a connection request, the server needs to start a thread for processing. If the connection does not do anything, it will cause unnecessary thread overhead. Of course, it can be improved through the thread pool mechanism.
Synchronous non blocking IO (Java NiO):
Synchronous non blocking. The server implementation mode is one request and one thread, that is, the connection requests sent by the client will be registered on the multiplexer. The multiplexer will start a thread for processing only when it polls that there are I / O requests connected. The user process also needs to ask whether the IO operation is ready from time to time, which requires the user process to keep asking.
Asynchronous blocking IO (Java NiO):
In this way, the application does not wait for the completion of the kernel IO operation after initiating an IO operation. After the kernel completes the IO operation, the application will be notified. In fact, this is the key difference between synchronization and asynchrony. Synchronization must wait or actively ask whether the IO is completed. Why is it blocked? At this time, it is completed through the select system call, and the implementation of the select function itself is blocked. The advantage of using the select function is that it can listen to multiple file handles at the same time (if from the perspective of unp, select is a synchronous operation, because the process still needs to read and write data after select), so as to improve the concurrency of the system!
(Java AIO (NiO. 2)) asynchronous non blocking IO:
In this mode, the user process only needs to initiate an IO operation and then return immediately. After the IO operation is really completed, the application will be notified of the completion of the IO operation. At this time, the user process only needs to process the data, and there is no need to carry out the actual IO read-write operation, because the real IO read-write operation has been completed by the kernel.
Applicable scenario analysis of bio, NiO and AIO:
Bio mode is applicable to the architecture with a small and fixed number of connections. This mode has high requirements for server resources, and concurrency is limited to applications. Jdk1 4 the only choice before, but the program is intuitive, simple and easy to understand. NiO mode is applicable to architectures with a large number of connections and relatively short connections (light operation), such as chat server. Concurrency is limited to applications and programming is complex, which is supported by jdk1.4. AIO mode is used to architectures with a large number of connections and relatively long connections (heavy operation), such as album server, which fully calls OS to participate in concurrent operation. Programming is complex, which is supported by JDK7.
The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.