Differences between synchronous call, callback, and asynchronous call

Source: Internet
Author: User

Synchronous call is a blocking call.
For example, the beacon fire of the ancient Great Wall transmits information. Now we assume that each beacon fire can only see adjacent beacon states, and each beacon fire is only bright and dark.
Now there are four beacon fires A, B, C, and D. A lights up first. B sees the beacon fire of A. It immediately ignited and took 2 seconds to light up. But at this time, the person responsible for the fire of C is sleeping, but at this time everyone is waiting for the light of C. Finally, C has slept for two hours and saw B light up and then lit up. D. Because it has not been lit for a long time, a problem occurs in the beacon fire. Therefore, the whole process is waiting for the completion of D.

if A completedo Belse wait Aif B completedo Celsewait Bif C completedo D elsewait C

This is a typical blocking mechanism. In any case, we can only wait for the completion of the previous task. If it is not completed, we can only continue to wait. The problem is that we have been wasting system resources.

Callback is a two-way call mode. That is, the called party also calls the interface of the other party when the interface is called.
The preceding example is as follows:
Now we are still a, B, c, d, but A has a pigeon (B) to B, and B has (C) and C has (d ). Now, after receiving the message, a immediately told pigeon B, and then lit up the beacon. The pigeon will bring the information to B, B will immediately light up upon receiving the information, and then put the pigeon to C, c. After seeing B light, I immediately told the pigeon, and then lit up the beacon fire. Then I received the message from pigeon C. Finally, D received the pigeon and lit it.
As you can see, there are two ways to do this. One is to put the pigeon (that is, execute the callback function first, and then continue to execute the following code), and then light up the beacon. You can also light up the beacon and then put the pigeon.

if A Messagesend to b do Aif B Message From bdo Bsend to cif B completesend to Ddo Cmessage from cif Message From ddo D

In this case, the function parameter carries another function pointer. When necessary, we can call other functions by using the function pointer name to send messages to other functions.

Asynchronous call
Asynchronous calling is a mechanism similar to a message or event, but its calling direction is the opposite. When an interface service receives a message or an event, the customer is notified (that is, the customer's interface is called ).
The above example is still used:
Now we have a minister F who is responsible for telling every beacon to light up.
A, B, c, d four beacon fires, the minister first told a, and then not waiting for a to shine, he continued to tell B, C and D. Finally, a told F that I had finished in 2 hours, B told F that I had finished in 1 hour, and C told F that I had finished in 1.5 hours, d told F 3 hours later. F after receiving the information, the whole process is completed.


F tell A  do A  time 2 hours  if A complete Report to FF tell B  do B time 1 hours  if B complete Report to FF tell C  do C time 1.5 hours  if C complete Report to FF tell D  do D  time 3 hours  if D complete Report to F

Although the four programs have a sequence of order, the lighting of the four beacon fires does not depend on whether or not the previous beacon fires are lit. That is to say, although our code execution is executed in sequence on the whole, we will not execute the code because it is not completed.

I will write three examples below. Let's take a look at the specific call method for each example.

Example 1:

var temp = false;while(!temp){  temp = wait(A.lightUp());}temp = false;while(!temp){  temp = wait(B.lightUp());}temp = false;while(!temp){  temp = wait(C.lightUp());}temp = false;while(!temp){  temp = wait(D.lightUp());}

Example 2:

function lightUp(A, callback){   callback();   A.lightUp();}function callback(){   B.lightUp();}

Example 3:

A.lightUp(){  this.do();  this.onComplete(this.tell(F));}B.lightUp(){  this.do();  this.onComplete(this.tell(F));}C.lightUp(){  this.do();  this.onComplete(this.tell(F));}D.lightUp(){  this.do();  this.onComplete(this.tell(F));}

The above three examples can be answered by yourself. I will not answer them here. If you want to answer these questions, you can comment on them. I hope you will not be able to understand them. Maybe I only think about it.

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.