The difference between synchronous programming and asynchronous programming

Here I will use a few examples (with pictures and texts) to explain to you the specific differences.

first example

1. Synchronization

Steps:

step1 ===》 step2 ===》step3

 First print hello moon,

Then print the picture,

Wait for the picture to be printed before printing hello Jupiter

Execute step by step, wait for the previous step to be executed, and then execute the next step.

Two, asynchronous

step1 ===》step2 / step3

 First print hello moon,

Then execute the print picture. If the network is slow when printing the picture, it will continue to print hello Jupiter

second example

Send a message to 100 customers and ask them what their passport number is. 

1. Synchronization

Send a message to a customer, wait for him to find the number and find it before replying to you, and you send another message until everyone's number is known. (One solid grid represents sending messages, and two dotted grids represent the waiting process.) During the waiting process, you have nothing to do but wait.

Two, asynchronous

After sending a message to a customer, while waiting for his reply, send a message to another customer, without waiting for him until you know everyone's number.

the difference

Asynchrony has significantly improved work efficiency, and now the mainstream is asynchronous requests, the purpose of which is to improve user experience.

But sometimes our task 3 needs to get some data from it after task 2 is completed. At this time, we need to add async and await to task 2.

Guess you like

Origin blog.csdn.net/DongShanYuXiao/article/details/131931090