[Go] understanding blocking non-blocking and synchronous asynchronous

Source: Internet
Author: User

from:http://www.zhihu.com/question/19732473

1. Synchronous and asynchronous
Synchronous and asynchronous attention is to the message communication mechanism (synchronous communication/asynchronous Communication)
The so-called synchronization is that when a * call is issued, the * call does not return until the result is obtained. But once the call returns, it gets the return value.
In other words, it is the result of the * caller actively waiting for this * call *.

Instead of async, the call is returned immediately after it isemitted , so no results are returned . In other words, when an asynchronous procedure call is made, the caller does not get the result immediately. Instead, the caller is notified by status, notification, or by a callback function to handle the call after the * call * is issued.

Typical asynchronous programming models like node. js

To cite a popular example:
You call the bookstore boss there is no "distributed System" this book, if it is a synchronous communication mechanism, the bookstore owner will say, you wait, "I check", and then start to check Ah, and so check (maybe 5 seconds, may be a day) to tell you the results (return results).
and the asynchronous communication mechanism, the bookstore owner directly told you I check Ah, check the phone to you, and then directly hung up (do not return the results). And then check it out and he'll call you on his own initiative. Here the boss calls back by "callback" this way.

2. Blocking and non-blocking
Blocking and non-blocking concerns the state of the program when it waits for the call result ( message, return value).

A blocking call means that the current thread is suspended until the call results are returned. The calling thread will not return until the result is obtained.
A non-blocking call means that the call does not block the current thread until the result is not immediately available.

Or the above example,
You call to ask the bookstore owner there is a "distributed System" this book, if you are blocking calls, you will always put yourself "hang" until the book has no results, if the non-blocking call, you don't care if the boss has told you, you have to play the first side, Of course, you'll have to check the boss for a few minutes occasionally.
Here the blocking is independent of the non-blocking and whether synchronous asynchronous. It has nothing to do with how the boss responds to your results.

--------------------------------------------------------------------------------------------------------------- --------------

Lao Zhang Love tea, nonsense do not say, boiled water.
Appearance characters: Lao Zhang, Kettle Two (ordinary kettle, referred to as a water bottle, the kettle will ring, referred to as the kettle).
1 Lao Zhang put the kettle on the fire and conforming the water. (Synchronous blocking)
Lao Zhang thinks he's a little silly.
2 Lao Zhang put the kettle on the fire, go to the living room to watch TV, and occasionally go to the kitchen to see if the water is open. (Synchronous non-blocking)
Lao Zhang still feel a little silly, then become high-end, bought a will ring flute of that kind of kettle. After the water is open, you can make a loud noise.
3 Old Zhang put the kettle on the fire and conforming the water. (Asynchronous blocking)
Lao Zhang thinks such a silly meaning is not very important
4 Old Zhang put the kettle on the fire, went to the living room to watch TV, the kettle rang no longer to see it, rang again to get the pot. (Asynchronous non-blocking)
Lao Zhang thought he was smart.


The so-called synchronous asynchronous, just for the kettle.
Ordinary kettle, synchronous; kettle, asynchronous.
Although all can work, but the kettle can be completed after their own, the old Zhang Water opened. This is not an ordinary kettle.
Synchronization can only allow callers to poll themselves (in case 2), resulting in inefficient old Zhang.

The so-called blocking non-blocking, only for the old Zhang.
The old Zhang of conforming, blocking; Old Zhang watching TV, non-blocking.
Situation 1 and situation 3 old Zhang is blocked, the daughter-in-law shouted he did not know. Although the 3-ring kettle is asynchronous, it does not make much sense to conforming's old Zhang. so asynchronous is generally used in conjunction with non-blocking, in order to play an asynchronous effect.

[Go] understanding blocking non-blocking and synchronous asynchronous

Related Article

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.