About asynchronous, synchronous, blocking, and non-blocking

Source: Internet
Author: User

If you want to have a kung pao chicken rice:

Synchronous congestion: you order food at the restaurant, and then wait there, and you have to shout: Okay!

Synchronous non-blocking: after ordering at the restaurant, you can walk the dog. But after a while, I went back to the restaurant and shouted: Okay!

Asynchronous blocking: when walking the dog, you receive a call from the restaurant saying that the meal is ready and you can get it yourself.

Asynchronous non-blocking: The restaurant calls us and says, "We know where you are, and we will send it to you later, so you can walk your dog with peace of mind.

The above Metaphor provides an image. Here is a description of the reactor and proactor modes, giving a more rigorous and clear analysis:

In the high-performance I/O design, there are two well-known modes: reactor and proactor. The reactor mode is used to synchronize I/O, while the proactor is used for asynchronous I/O operations. Before comparing these two modes, we should first understand several concepts, what is blocking and non-blocking, and what is synchronous and asynchronous, synchronization and Asynchronization are for the interaction between applications and the kernel. Synchronization means that the user process triggers Io operations and waits or polls to check whether the IO operations are ready, asynchronous means that the user process starts to do his/her own things after triggering the IO operation, and when the IO operation is completed, it will receive the notification of Io completion (Asynchronous is characterized by notification ). Blocking and non-blocking are different methods for processes to access data according to the ready status of Io operations. To put it bluntly, they are an implementation method for reading or writing operation functions, reading or writing a function in blocking mode will wait until the function is read or written, but not in blocking mode, a status value will be returned immediately after the function is read or written. In general, I/O models can be divided into synchronous blocking, synchronous non-blocking, asynchronous blocking, asynchronous non-blocking Io synchronous blocking IO: in this way, after a user process initiates an IO operation, you must wait for the completion of the I/O operation. Only after the I/O operation is completed can the user process run. This method is applicable to traditional Java I/O models! Synchronous non-blocking IO: In this mode, the user process can return other tasks after initiating an IO operation, but the user process needs to ask from time to time whether the IO operation is ready, this requires the user process to keep asking, so as to introduce unnecessary CPU resource waste. Currently, Java NiO is a non-blocking synchronous Io. Asynchronous Io blocking: after an application initiates an I/O operation, it does not wait for the completion of the kernel I/O operation. The application will be notified after the kernel completes the I/O operation, this is actually the key difference between synchronization and Asynchronization. Synchronization must wait or actively ask whether Io is completed. Why is it blocking? In this case, the Select function is called by the Select system, and the implementation method of the select function is blocking, the benefit of using the select function is that it can listen to multiple file handles at the same time (from the perspective of UNP, select is a synchronization operation. Because after the SELECT statement, the process also needs to read and write data) to improve the concurrency of the system! Asynchronous non-blocking IO: In this mode, a user process only needs to initiate an IO operation and then return immediately. After the IO operation is 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 does not need to perform actual Io read/write operations, because the real Io read or write operations have been completed by the kernel. Currently, Java does not support this Io model. After figuring out the above concepts, let's look back at the reactor mode and proactor mode. (In fact, both blocking and non-blocking can be understood as the concept of synchronization. For Asynchronization, non-blocking will not be implemented. After receiving an asynchronous notification, the user process can directly operate the data in the user State Space of the process .) First, let's take a look at the reactor mode. The reactor mode is applied to synchronous I/O scenarios. Take the read operation and write operation as examples to see the specific steps in the reactor: read operation: 1. the application registers the read-ready event and the associated event Processor 2. event splitter waits for the occurrence of an event. 3. when a read-ready event occurs, the event splitter calls the event processor registered in step 1. 4. the event processor first executes the actual read operation, and then further processes the write operation based on the read content, similar to the read operation, but the first step registers the write-ready event. Next, let's take a look at the read and write operations in proactor mode: Read operations: 1. the application initializes an asynchronous read operation and registers the corresponding event processor. At this time, the event processor does not focus on the read-ready event, but on the read completion event, which is different from the reactor. 2. the event splitter waits for the read operation to complete the 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 buffer Supply Program operation that the operating system is responsible for reading and writing data to the application, the operating system plays an important role), and puts the read content into the cache zone passed by the user. This is also different from the reactor. In proactor, the application needs to pass the cache area. 4. After the event splitter captures the read completion event, it activates the event processor registered by the application. The event processor directly reads data from the cache without actual read operations. Write and read operations in proactor, but the event of interest is the write completion event. From the above, we can see that the main difference between the reactor and proactor modes is who completes the real read and write operations. In the reactor, the application needs to read or write data by itself, in proactor mode, an application does not need to read or write data from the cache, the operating system reads the cache or writes the cache to the real I/O device. to sum up, synchronization and Asynchronization are relative to the interaction mode between the application and the kernel. synchronous requests need to be initiated, while asynchronous requests notify the application when the kernel sends an IO event, blocking and non-blocking are only the implementation methods of functions when the system calls the system.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.