Distributed System Concern (20) – What’s the difference between blocking and non-blocking?

Time:2019-9-12

If you see my article for the second time, welcomeSubscribe to my personal public number at the end of the article (Cross-border Architect)Yo!
The public number is delivered on time at 8 a.m. every Friday. Of course, there will also be an additional meal from time to time

In the previous article, Brother Z talked to you about the meaning of “asynchrony” and how to use it. If you miss this article, you can take a look at it first.

In fact, I know that many small partners tend to confuse “asynchronous” with “non-blocking”. The impression may be that asynchronism = non-blocking, synchronization = blocking?

Actually, that’s not the case. Brother Z, I want to help you figure out this problem this time.

Synchronization and blocking/non-blocking

Most of the “synchronous” calls in your usual code are essentially “blocking”. However, a “synchronous” call can also achieve a “non-blocking” effect.

Let’s take the example of queuing for milk tea mentioned in our last article and see why it’s called “synchronization”+ “congestion”.

The example of “synchronization” in this article is that you queue up to buy milk tea, order the order and continue to “occupy the pit”, not let the people behind order, wait for the clerk inside to make milk tea, you take the back order. This is actually “synchronization”+ “blocking”. Where is “blocking”?

Distributed System Concern (20) - What's the difference between blocking and non-blocking?

Because at this time you have been “occupying the pit” for fear that the person behind will order the order first, leading the shop assistant to do it for him first. So, at this point, you stare inside, which is “blocking” because you can’t do anything but stare at it.

How can “synchronization” be allowed without blocking?

Even though you are still in line to “occupy the pit”, but people are not idle, bowing their heads to play with mobile phones, and from time to time ask inside, “My milk tea is ready? Is my milk tea ready yet? This is “non-blocking” because there is a time lag between your two inquiries and you can do something else at this time. Essentially, it achieves the effect of “non-blocking” by splitting the original “big synchronization” into multiple “small synchronization”.

Distributed System Concern (20) - What's the difference between blocking and non-blocking?

In the figure above, the blank area between several blockages can be used to do other things, so it is “non-blocking”.

Asynchronism and blocking/non-blocking

The example of “asynchrony” in the previous article is a “non-blocking” example. Let’s see why.

After the milk tea shop has ordered a single area and a dining area, the drinks prepared can only be taken from the dining area, which means that the person who receives your order is not the one who actually makes milk tea. At this time, you will get a dining number, and then honestly go to the dining area to wait, rather than “occupy XX not xx”.

If you are in a hurry to get milk tea, keep asking, “Is my milk tea ready yet?” Is my milk tea ready yet? This is also a “synchronous” and “non-blocking” mode.Because this process does not produce “callbacks”, you are constantly initiating “requests”.

But if you’re not in a hurry, just start a chicken game on the side and wait for the calls to be made inside, and then go get your number. This is “asynchronous” + “non-blocking”.Because this is triggered by the other party (the clerk inside), which is called “callback” and is a response to your previous “order” request.。 Complete a complete interaction one at a time.

Distributed System Concern (20) - What's the difference between blocking and non-blocking?

So maybe you would say that asynchrony is not natural “non-blocking”? No, No, No.

Blocking is not your own decision, you can block ah. For example, when you wait for “callback”, you find that you can’t play chicken without your cell phone. You can only wait there foolishly and do nothing. In this way, although the process is still “asynchronous”, it is “blocking” for you.

Synchronization/Asynchronization-Blocking/Non-Blocking in Work

Synchronization + Blocking. This is the most common, most of the code written in peacetime is the same, let alone say.

In fact, if you think about it carefully, you will find that many well-known frameworks are “synchronous” and “non-blocking”. Why? Because you can continue to write code like synchronization, but you can enjoy better performance like asynchrony. Why not?

For example, poll/select/epoll, a well-known IO reuse model in linux, is essentially “synchronous” and “non-blocking”. There is also Netty, a well-known network communication framework.

We can also use this mode when designing external APIs to reduce the impact of some time-consuming interface calls. This teacher Ruan Yifeng has written very clearly, so I will post a link directly: http://www.ruanyifeng.com/blo….

It’s not unreasonable to think that “asynchrony” means “non-blocking”. Why? Because I searched in my mind, I did not expect any well-known frameworks / designs to be implemented using “asynchronous” and “blocking”. If any of your partners have additional information, you can leave a message in the comments section.

Asynchronism + non-blocking is much more. Any framework that you see the callback keyword is.

summary

Okay, let’s sum up.

This time, Brother Z first talked about the four combinations of synchronous/asynchronous, blocking/non-blocking.

Then talk to you about where you can see them in your work and what patterns are appropriate for some typical scenarios.

I hope it will inspire you.

Finally, give you the best way to remember these four concepts.

  • Synchronized Blockage: Go ahead, I’ll watch you do it.
  • Synchronized non-blocking: You go ahead, I’ll see it every five minutes.
  • Asynchronous Blockage: Go ahead, tell me, I’ll wait.
  • Asynchronous non-blocking: You go ahead, tell me, I’m going to do something else first.

If you still can’t remember, rememberSynchronization/asynchronization denotes “process” and blocking/non-blocking denotes “state” in the process.。 As for how this sentence came into being, just come back and read this article.

Distributed System Concern (20) - What's the difference between blocking and non-blocking?

Related articles:

  • Distributed System Focus: Deep and Simple “Asynchronism”
  • Distributed System Focus: 360 Degree Overall Interpretation of Cache

Author: Zachary

Origin: https://www.cnblogs.com/Zacha…


If you like this article, you can click on the end of the article.Fabulous」。

This will give me some feedback. )

Thank you for your efforts.

About the author: Zachary,Personal Microsignal: Zachary-ZF) Persist in polishing every piece of high quality original. WelcomeScan belowTwo-dimensional code ~.
Publish original content regularly:Some Thoughts on Architecture Design, Distributed System, Product Operation

If you’re a junior programmer, you want to improve but don’t know how to do it. Or, as a programmer for many years, I have been stuck in some bottlenecks and want to broaden my horizon. Welcome to my public number.Cross-border ArchitectReply.technology“Give you a mind map that I have collected and sorted out for a long time.

If you are operating, there is nothing you can do in the face of changing markets. Or want to understand the mainstream operation strategy, in order to enrich their own “warehouse”. Welcome to my public number.Cross-border ArchitectReply.Operate“Give you a mind map that I have collected and sorted out for a long time.

Distributed System Concern (20) - What's the difference between blocking and non-blocking?