【Python基础】并发,并行,Concurrency vs. Parallelism

The terms concurrency and parallelism are often used in relation to multithreaded programs. But what exactly does concurrency and parallelism mean, and are they the same terms or what?

The short answer is "no". They are not the same terms, although they appear quite similar on the surface. It also took me some time to finally find and understand the difference between concurrency and parallelism. Therefore I decided to add a text about concurrency vs. parallelism to this Java concurrency tutorial.

Concurrency

Concurrency means that an application is making progress on more than one task at the same time (concurrently). Well, if the computer only has one CPU the application may not make progress on more than one task at exactly the same time, but more than one task is being processed at a time inside the application. It does not completely finish one task before it begins the next.

并发的反义词是顺序,concurrency vs sequential,例如:
顺序处理
你陪女朋友先看电影(Task1),看完后陪女朋友到花店买了一束花(Task2),然后陪女朋友去西餐厅吃烛光晚餐(Task3),这就是“顺序处理”,因为整个过程中只有你这一个处理器,事情只能一件一件的做(要么是你亲自做,要么你要等别人做)。Task1你要花2小时,Task2等花做好你要花30分钟,Task3等菜做好要30分钟,从你开始看电影到开始吃饭,全程需要3小时(假设走路不算时间)。

并发处理
你陪女朋友先看电影(Task1),同时打电话给花店预定一束花,花店安排人员在20:00送到西餐厅(Task2);同时你打电话给西餐定预定20:00的浪漫烛光晚餐,西餐厅开始给你准备晚餐(Task3);等到你电影看完跑到西餐厅,花也送到了,晚餐也准备好了,你跑过去直接献花吃饭然后开房即可,这就是并发处理。Task1还是2小时,但Task2和Task3也在这2小时完成了,从你开始看电影到开始吃饭,全程只需要2小时,3个任务是并发完成的。秘诀就是有3个处理器了:你、花店、餐厅在同一个时间段内都在做各自的任务。

Parallelism

Parallelism

Parallelism means that an application splits its tasks up into smaller subtasks which can be processed in parallel, for instance on multiple CPUs at the exact same time.


并行的反义词是串行,Parallelism vs Serial,比如说给你一个100万的整形数组,挑出其中最小的值。
串行处理
从数组的第一个开始扫描到最后一个,类似冒泡排序一样
并行处理
将数组分为10组,每组10万个整形,同时扫描10组得到10个数值,然后再将这10个数值排列一下。
上面这个简单的例子也可以看出,串行改为并行其实并不那么简单,涉及到任务分解(有先后依赖的任务就不能做到并行)、任务运行(可能要考虑互斥、锁、共享等)、结果合并。

Concurrency vs. Parallelism In Detail

As you can see, concurrency is related to how an application handles multiple tasks it works on. An application may process one task at at time (sequentially) or work on multiple tasks at the same time (concurrently).

Parallelism on the other hand, is related to how an application handles each individual task. An application may process the task serially from start to end, or split the task up into subtasks which can be completed in parallel.

As you can see, an application can be concurrent, but not parallel. This means that it processes more than one task at the same time, but the tasks are not broken down into subtasks.

An application can also be parallel but not concurrent. This means that the application only works on one task at a time, and this task is broken down into subtasks which can be processed in parallel.

Additionally, an application can be neither concurrent nor parallel. This means that it works on only one task at a time, and the task is never broken down into subtasks for parallel execution.

Finally, an application can also be both concurrent and parallel, in that it both works on multiple tasks at the same time, and also breaks each task down into subtasks for parallel execution. However, some of the benefits of concurrency and parallelism may be lost in this scenario, as the CPUs in the computer are already kept reasonably busy with either concurrency or parallelism alone. Combining it may lead to only a small performance gain or even performance loss. Make sure you analyze and measure before you adopt a concurrent parallel model blindly.

猜你喜欢

转载自blog.csdn.net/wangpengfei163/article/details/80390838