今天看到一个对Concurrency
(并发)和Parallelism
(并行)这两个术语的一个解释,感觉很精确。记录在此,以供日后查阅(原文在这里):
Concurrency and parallelism are two related but distinct concepts.
Concurrency means, essentially, that task A and task B both need to happen independently of each other, and A starts running, and then B starts before A is finished.
There are various different ways of accomplishing concurrency. One of them is parallelism--having multiple CPUs working on the different tasks at the same time. But that's not the only way. Another is by task switching, which works like this: Task A works up to a certain point, then the CPU working on it stops and switches over to task B, works on it for a while, and then switches back to task A. If the time slices are small enough, it may appear to the user that both things are being run in parallel, even though they're actually being processed in serial by a multitasking CPU.
Concurrency
(并发)是指系统上的多个task
(任务)之间由于没有任何依赖关系,所以可以同时运行。以两个task
(A
和B
),两核CPU
(A
和B
)系统为例,如果在一个时间点上,task A
在CPU A
上运行,而task B
在CPU B
上运行,我们就可以称这两个task
是Parallelism
(并行)运行的。对单核CPU
来说,由于在一个时间点上只能执行一个task
,所以这两个task
只能是Sequential
(串行)运行的。
一句话总结,Concurrency
(并发)描述了task
之间的逻辑关系(能否同时运行),而Parallelism
则描述了task
运行时的真正状态(是否真的在同时运行)。