[Switch] synchronous, asynchronous, blocking, and non-blocking

Source: Internet
Author: User

1. synchronous and asynchronous

Synchronous and asynchronous focuses onMessage Communication Mechanism (synchronous communication/asynchronous communication)

The so-calledSynchronizationWhen a * call * is sent, the * call * is not returned until the result is not obtained. However, once the call returns, the returned value is returned.

In other words, the * caller * takes the initiative to wait for the * call * result.

WhileAsynchronousOn the contrary, * call * directly returns the result after the call is sent, so no result is returned. In other words, when an asynchronous process call is sent, the caller will not immediately get the result. However, after * call * is sent, * the * called * notifies the caller through the status or notification, or processes the call through the callback function.

For example:

You can call the bookstore owner to check whether the book "Distributed System" is available. If it is a synchronous communication mechanism, the bookstore owner will say, wait a moment, "I will check", and then start to check, wait until the query is completed (may be 5 seconds or one day) to tell you the result (return result ).

In the asynchronous communication mechanism, the bookstore owner directly told you to check the mechanism. After checking the mechanism, he called you and hung up the phone directly (no result is returned ). After checking the information, he will take the initiative to call you. Here, the boss calls back and forth through "power-Back.

2. Blocking and non-blocking

Blocking and non-blocking focuses onThe status when the program is waiting for the call result (message, return value.

Blocking callThe current thread is suspended before the call result is returned. The call thread returns the result only after the result is obtained.

Non-blocking callThe call does not block the current thread until the result is immediately obtained.

Or the above example,

You call the bookstore owner to check whether the book "Distributed System" exists. If you call the book in blocking mode, you will always "Suspend" yourself until you get the result of the book, if it is a non-blocking call, whether the boss tells you or not, you can play it on your own. Of course, you also need to check whether the boss has returned the result in a few minutes.

Blocking and non-blocking are irrelevant to synchronous Asynchronization. It has nothing to do with how the boss answers your results.


Attach anotherEasy to understand examples:

Old Zhang loves drinking tea, without talking nonsense, and boiling water.

Appearance character: Lao Zhang, two kettle (ordinary kettle for short; ring kettle for short ).

1. Old Zhang put the kettle on the fire and waited for the water to open. (Synchronous blocking)

Lao Zhang thinks he is a bit silly.

2. Old Zhang put the kettle on the fire and went to the living room to watch TV,From time to timeGo to the kitchen to see if the water is open. (Non-blocking synchronization)

Lao Zhang still thought he was a little silly, so he became a high-end user and bought the kind of kettle that would sound the flute. When the water is turned on, it can send out loud noises ~~~~ Noise.

3. Old Zhang put the kettle on the fire and waited for the water to open. (Asynchronous blocking)

Lao Zhang thinks it is of little significance to be stupid.

4. Lao Zhang put the kettle on the fire and went to the living room to watch TV. Before the kettle rang, he no longer went to watch it. Then he went to get the kettle. (Asynchronous non-blocking)

Lao Zhang thinks he is smart.

The so-called synchronous Asynchronization is only for the kettle.

Ordinary kettle, synchronous; kettle, asynchronous.

Even though they can work, the kettle can be completed by themselves, prompting Lao Zhang to open the water. This is not what ordinary kettle can do.

Synchronization only allows callers to poll themselves (in Case 2), resulting in low efficiency.

The so-called non-blocking is only for Lao Zhang.

Li and other old Zhang, blocking; watch TV Old Zhang, non-blocking.

In case 1 and case 3, Lao Zhang is blocked, and his daughter-in-law shouted that he did not know. Although the kettle in step 3 is asynchronous, it does not make much sense for lahang. SoGenerally, Asynchronization works with non-blocking operations to make full use of Asynchronization.


References: http://www.cnblogs.com/jdflyfly/p/3874895.html


[Switch] synchronous, asynchronous, blocking, and non-blocking

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.