In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article shows you what is the difference among NIO, BIO and AIO. The content is concise and easy to understand. It will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.
In the design of high-performance IO systems, there are several noun concepts that often confuse us. The details are as follows:
1. Synchronization and asynchronism are for the interaction between the application and the kernel. Synchronous / asynchronous are two processing strategies that emphasize the result / opportunity cost of dealing with things in time; emphasizing the result means not waiting for the result, but whether the result is right or wrong, anyway, you have to give me a result response immediately. Emphasizing the opportunity cost of time means that it is extremely difficult to accept the time wasted waiting for the result, but not so eager for the result. For the time being, ignore the result (let the processor take the initiative to get the result when he or she is free) and deal with something else instead.
2. Blocking and non-blocking are different ways for the process to access data according to the ready state of the IO operation. To put it bluntly, it is a way to implement the read or write operation function. In the blocking mode, the read or write function will always wait, while in the non-blocking mode, the read or write function will immediately return a status value. 3, synchronous / asynchronous is macroscopical (inter-process communication, usually manifested as the processing of network IO), blocking / non-blocking is microcosmic (intra-process data transmission, usually manifested in the processing of local IO); blocking and non-blocking are the forms of synchronous / asynchronous, which can be summed up in a short sentence, synchronous and asynchronous are the purpose, blocking and non-blocking are the ways of implementation.
Numbered noun explanation example 1 synchronization means that the user process triggers the IO operation and waits or polls to see if the IO operation is ready to go shopping for clothes, do it yourself, and can't do anything else. 2 Asynchronous Asynchronous means that the user process starts to do its own things after triggering the IO operation, and when the IO operation has been completed, it will be notified of the completion of the IO (the characteristic of async is the notification) to tell friends that they are the right size, size and color of the clothes, and then they can do something else. (when using asynchronous IO, Java delegates IO read and write to OS, and needs to pass the data buffer address and size to OS.) 3 blocking means that when trying to read and write to the file descriptor, if there is nothing to read or write at that time, the program goes into a waiting state until there is something to read or write to go to the bus station to recharge. The recharger is not here (he may have gone to the toilet), and then we wait here until the recharger comes back. Of course, this is not the case in the real world, but it is true in computers. (4) in the non-blocking and non-blocking state, if there is nothing to read or cannot write, the read-write function will return immediately instead of waiting. When you withdraw money from the bank for business, you will get a small ticket. After receiving it, we can play with our mobile phone or chat with others. When it is our turn, the bank horn will notify us, and then we can go.
Now let's understand the IO type of the combination mode, which is much easier to understand.
Synchronous blocking IO (JAVA BIO): synchronous and blocking, the server implementation mode is a connection to a thread, that is, when the client has a connection request, the server needs to start a thread to process, if this connection does nothing 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 thread per request, that is, all connection requests sent by the client are registered with the multiplexer, and the multiplexer starts a thread for processing only when the multiplexer polls that the connection has an Icano request. 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): this means that after the application initiates an IO operation, it does not wait for the completion of the kernel IO operation, and after the kernel completes the IO operation, the kernel will notify the application. This is actually the most critical difference between synchronous and asynchronous. Synchronization must wait or actively ask whether the IO is completed, so why is it blocked? Because this is done through select system calls, and the implementation of the select function itself is blocked, one advantage of using the select function is that it can listen on multiple file handles at the same time (if from a UNP point of view, select is a synchronous operation. Because after select, the process also needs to read and write data), thus improving 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 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 for the actual IO read and write operation, because the real IO read or write operation has been completed by the kernel.
Analysis of applicable scenarios for BIO, NIO and AIO:
The BIO method is suitable for the architecture with a small number of connections and a fixed number of connections, which requires high server resources, and concurrency is limited to the application. JDK1.4 is the only choice before, but the program is intuitive, simple and easy to understand.
NIO is suitable for architectures with a large number of connections and short connections (light operation), such as chat servers, where concurrency is limited to applications, programming is more complex, and JDK1.4 begins to support it.
AIO is used in architectures with a large number of connections and long connections (heavy operations), such as photo album server, fully calling OS to participate in concurrent operations, programming is more complex, and JDK7 begins to support it.
After figuring out the above concepts, let's go back to the Reactor pattern and the Proactor pattern.
In fact, both blocking and non-blocking can be understood as concepts that exist only in the context of synchronization. For asynchronism, blocking and non-blocking will no longer be divided. For the user process, after receiving the asynchronous notification, you can directly manipulate the data in the user state space of the process. )
First, let's take a look at the Reactor mode, which is applied to the scenario of synchronizing Imax O. Let's take a read operation and a write operation as examples to look at the specific steps in Reactor:
Read operation:
1. The application registers read-ready events and associated event handlers
two。 The event splitter waits for the event to occur
3. When a read-ready event occurs, the event splitter calls the event handler registered in the first step
4. The event handler first performs the actual read operation, and then further processes the write operation based on the read content, similar to the read operation, except that the first step is to register the write-ready event.
Let's take a look at the read and write operations in Proactor mode:
Read operation:
1. The application initializes an asynchronous read operation and then registers the corresponding event handler, which focuses not on the read ready event but on the read completion event, which is the key difference from Reactor.
two。 Event splitter waits for read operation to complete event
3. When the event splitter waits for the read operation to complete, the operating system calls the kernel thread to complete the read operation (asynchronous IO is the operating system responsible for reading and writing data to the buffer passed by the application for application operation, and the operating system plays an important role), and puts the read content into the cache passed by the user. This is also different from Reactor, where applications need to pass caches in Proactor.
4. After the event splitter captures the read completed event, it activates the event handler registered by the application, and the event handler reads the data directly from the cache without the actual read operation.
Write and read operations in Proactor, except that the event of interest is the write completion event.
As can be seen from the above, the main difference between Reactor and Proactor mode is who does the real read and write operation. In Reactor, the application needs to read or write data by itself, while in Proactor mode, the application does not need to read or write from the cache, and the operating system will read or write the cache to the real IO device.
To sum up, synchronization and asynchronism are relative to the interaction between the application and the kernel, synchronization needs to be asked actively, while when asynchronous, the kernel notifies the application when IO events occur, and blocking and non-blocking are only the ways in which the system implements functions when calling system calls.
If you want to have a Kung Pao chicken meal:
Synchronous blocking: you go to the restaurant to order, then wait there, and yell, "are you ready?"
Synchronous non-blocking: after ordering in a restaurant, you go to walk the dog. But after walking for a while, he went back to the restaurant and shouted, "are you ready?"
Asynchronous blocking: when walking your dog, you get a call from the restaurant saying that the meal is ready and ask you to get it yourself.
Asynchronous non-blocking: the restaurant called and said, we know your location, we will send it to you later, just walk the dog at ease.
"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 reads and writes block the request process, it is synchronous IO.
The difference between blocking IO and non-blocking IO lies in whether the originating IO request will be blocked in the first step, traditional blocking IO if blocking until completion, and non-blocking IO if not blocking.
Synchronization and asynchronism are for the interaction between the application and the kernel. Synchronization means that the user process triggers the IO operation and waits or polls to see if the IO operation is ready, while asynchronous means that the user process starts to do its own thing after triggering the IO operation, and will be notified of the completion of the IO operation when the IO operation has been completed. Blocking and non-blocking are different ways for the process to access data according to the ready state of the IO operation. To put it bluntly, it is a way to implement the read or write operation function. In the blocking mode, the read or write function will always wait, while in the 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 (that is, early IO operations), synchronous non-blocking (NIO), and asynchronous (AIO).
Synchronous blocking:
In this way, the user process must wait for the completion of the IO operation after initiating an IO operation, and the user process can run only after the IO operation is actually completed. JAVA's traditional IO model belongs to this approach.
Synchronous non-blocking:
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. The current NIO of JAVA belongs to synchronous non-blocking IO.
Async:
In this way, it means that after the application initiates an IO operation, it does not wait for the completion of the kernel IO operation and notifies the application after the kernel completes the IO operation. "
What is the difference among NIO, BIO and AIO? have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.