Asynchronous vs synchronous execution, what does i

2018-12-31 01:16发布

What is the difference between asynchronous and synchronous execution?

21条回答
笑指拈花
2楼-- · 2018-12-31 01:26

When you execute something synchronously, you wait for it to finish before moving on to another task. When you execute something asynchronously, you can move on to another task before it finishes.

That being said, in the context of computers this translates into executing a process or task on another "thread." A thread is a series of commands (a block of code) that exists as a unit of work. The operating system can manage multiple threads and assign a thread a piece ("slice") of processor time before switching to another thread to give it a turn to do some work. At its core (pardon the pun), a processor can simply execute a command, it has no concept of doing two things at one time. The operating system simulates this by allocating slices of time to different threads.

Now, if you introduce multiple cores/processors into the mix, then things CAN actually happen at the same time. The operating system can allocate time to one thread on the first processor, then allocate the same block of time to another thread on a different processor. All of this is about allowing the operating system to manage the completion of your task while you can go on in your code and do other things.

Asynchronous programming is a complicated topic because of the semantics of how things tie together when you can do them at the same time. There are numerous articles and books on the subject; have a look!

查看更多
不流泪的眼
3楼-- · 2018-12-31 01:27

As a really simple example,

SYNCHRONOUS

Imagine 10 school students instructed to walk as a queue on a road.

The 3rd student got her shoelace untied. Now she has stopped and tying up again.

All students behind her have stopped, and are waiting now for her to get it tied. The 1st and 2nd students have walked past them all, and continuing in their usual pace.

10-->9-->8-->7-->6-->5-->4-->3.     2-->1-->

ASYNCHRONOUS

Just Imagine 10 random people walking on the same road. They're not on a queue of course, just randomly walking on different places on the road in different paces.

3rd person's shoelace got untied. She stopped to get it tied up again.

But nobody is waiting for her to get it tied. Everyone else is still walking the same way they did before, in that same pace of theirs.

10-->    9-->
   8--> 7-->   6-->
 5-->  4-->  3. 2-->
1-->
查看更多
十年一品温如言
4楼-- · 2018-12-31 01:30

In regards to the "at the same time" definition of synchronous execution (which is sometimes confusing), here's a good way to understand it:

Synchronous Execution: All tasks within a block of code are all executed at the same time.

Asynchronous Execution: All tasks within a block of code are not all executed at the same time.

查看更多
一个人的天荒地老
5楼-- · 2018-12-31 01:30

A different english definition of Synchronize is Here

Coordinate; combine.

I think that is a better definition than of "Happening at the same time". That one is also a definition, but I don't think it is the one that fits the way it is used in Computer Science.

So an asynchronous task is not co-coordinated with other tasks, whereas a synchronous task IS co-coordinated with other tasks, so one finishes before another starts.

How that is achieved is a different question.

查看更多
后来的你喜欢了谁
6楼-- · 2018-12-31 01:32

When executing a sequence like: a>b>c>d>, if we get a failure in the middle of execution like:

a
b
c
fail

Then we re-start from the beginning:

a
b
c
d

this is synchronous

If, however, we have the same sequence to execute: a>b>c>d>, and we have a failure in the middle:

a
b
c
fail

...but instead of restarting from the beginning, we re-start from the point of failure:

c
d

...this is know as asynchronous.

查看更多
长期被迫恋爱
7楼-- · 2018-12-31 01:32

Synchronous : When a task is executed synchronously, you wait for a task to be completed before moving on to another task. One task depends on the end of another

Asynchronous : When a task is executed asynchronously, you can directly switch to another task before the previous has been completed. One task does not depend on the other.

In programming, tasks are executed on a "thread". It is necessary to understand that several threads can be created for tasks in order to avoid stacking in a single queue.

That being said, the concept of synchronous and asynchronous do not have much to do with threads, the reason I mention it is that it is rare to see tasks running asynchronously on a single thread (although technically possible).

查看更多
登录 后发表回答