You can understand synchronous and asynchronous, blocking and non-blocking after reading

Preface

I have seen many articles about synchronization and asynchrony, blocking and non-blocking on the Internet, but many of them throw out a bunch of related definitions. After reading them, they are still in the cloud, and these concepts are still not very good. To distinguish them. This article gives you a deep understanding of its essence through easy-to-understand language and related examples.

Synchronous and asynchronous

First of all, we must make it clear that both synchronization and asynchrony are for two or more things. For example, when we fancy an item in online shopping, and then browse the product details, first the page will send a request, the background server queries the relevant data of the corresponding product, and then the front-end details page will display the product based on the returned data details. At this time, your internet speed is relatively poor, and it took nearly a minute for a detailed page to be displayed. At this time, you ask whether the request is synchronous or asynchronous? The answer is obviously a synchronous request. The most intuitive form it gives us is that the page is always displayed during loading. The rendering of the product detail page must wait for the backend server to return the product detail data before proceeding. In other words, the next operation must wait for the completion of the previous operation before proceeding, and it depends on the return result of the previous operation.

You may ask, in the case of synchronization, when one thing is operating, what are other things doing at this time? In fact, there is no clear stipulation. In fact, synchronization is more about the process of serial execution of things one by one, ensuring that they will not be cross-executed. As for the state at a certain moment, it does not care. Most of the time in the computer, other things are in a waiting state, but we are much more flexible. In our daily life, the common synchronization method is queuing. For example, when we take the subway to and from get off work for security check, we need Line up for security check in order to get in the train, but you can look at your phone, chat or do nothing while you are in line. The security check staff will not care what you are doing. This is the synchronization caused by the limited security check resources.

You can understand synchronous and asynchronous, blocking and non-blocking after reading

There are two points to pay attention to for synchronization. One is the scope of synchronization. Sometimes global desynchronization is not needed. It only needs to synchronize in specific operations. This can improve execution efficiency, such as the Java language Synchronization method and synchronization code block. The other is the granularity of synchronization. Synchronization is not required for some large-scale operations. Small-granularity operations also require synchronization. It is just that some small-grained operations are naturally synchronized operations and do not require us to do it artificially. Add synchronization operation control. For example, synchronization in the Java language is for programs with two or more threads, because it is naturally synchronized in a single-threaded program. Asynchrony is the complete opposite. In asynchronous situations, multiple transactions can be carried out at the same time without affecting each other. You do yours, I do mine, and no one cares about each other. In general:

Synchronization Two things depend on each other, and one thing must depend on the execution result of the other thing. For example, in the thing A->B event model, you need to complete thing A before executing thing B. In other words, the synchronous call does not return before the callee has processed the request, and the caller will always wait for the return of the result.

Asynchronous two things are completely independent, the execution of one thing does not need to wait for the execution of the other thing. In other words, asynchronous calls can return results without waiting for the results to be returned. When the results are returned, use the callback function or other methods to carry the call results and then do related things.

It can be seen that synchronization and asynchrony describe things from the perspective of behavior. (PS: Multiple transactions here can refer to different operations, different methods, or different code statements, etc.)

Blocking and non

- blocking The so - called blocking, in simple terms is that a request cannot be returned immediately, and the response cannot be returned until all the logic is processed. On the contrary, if a request is issued, a response is returned immediately, without waiting for all the logic to be processed. Blocking and non-blocking refer to whether there is no operation in place when a single thread encounters a synchronous wait. Traffic jams are the best examples of blocking and non-blocking. Friends who have lived in first-tier cities should have experienced that cars can pass normally when traffic is normal, which is non-blocking. Traffic jams often occur during rush hours, and when traffic is normal It takes half an hour to drive, and it may take two or three hours to arrive during peak periods. . . And once a traffic jam occurs, all the cars on the road are motionless, and they can only wait in the car, which is blocked. Of course, most people will not choose to wait. They will play mobile phones or chat with friends, etc., the same on the computer Here, blocking means stopping execution, stopping and waiting, non-blocking means that the operation can continue to execute downwards, but when blocking occurs, the computer is not as flexible as humans. Usually, the computer processing method is to suspend the current thread, and then Wait until the block is over before continuing to execute the thread. You can see the current state of things described by blocking and non-blocking (the state while waiting for the result of the call).

You can understand synchronous and asynchronous, blocking and non-blocking after reading

Combining the synchronization and asynchrony described earlier, there will be four situations in the pairwise combination, namely synchronous blocking, synchronous non-blocking, asynchronous blocking and asynchronous non-blocking. The following uses the example of lanes to explain these states vividly:

There is only one lane for simultaneous blocking, and no overtaking is allowed. All cars run in sequence, and only one car can pass at a time. The embarrassing thing is that this lane is still blocked.

There is only one lane for synchronous non-blocking, and no overtaking is allowed. All cars run in sequence, and only one car can pass at a time. Fortunately, there is no traffic jam in this lane and can pass normally.

Asynchronous blocking has two or more lanes, each road can pass, and cars in different lanes can run in parallel. The embarrassing thing is that all lanes are in traffic jam.

Asynchronous non-blocking has two or more lanes, each road can pass, and cars in different lanes can run in parallel, but fortunately, there is no traffic jam in one lane, and all can pass normally.

Corresponding to our computer is the same, synchronous blocking is equivalent to only one thread, and the thread is in a blocking state, and synchronous non-blocking is equivalent to only one thread, and the thread is in a running state. Asynchronous blocking is equivalent to having multiple threads, and all threads are in a blocking state, and asynchronous non-blocking is equivalent to having multiple threads, and all threads are running normally.


to sum up

Many procedural ideas come from life, and we need to find the scenes around us by analogy, thinking, and summarizing, so that we can understand more deeply.

Guess you like

Origin blog.51cto.com/15075507/2607586