一、前言
看了很多文章都没有讲好这个问题, 一会一个火车的例子, 一会一个白菜的例子,看了也不知所云。找到一篇从CPU的角度讲这个问题的文章,抄下来了。
https://howtodoinjava.com/java/multi-threading/concurrency-vs-parallelism/
二、并发和并行
首先,并发需要存在多个任务,其次,这些任务在重叠的时间段内以无序的方式启动,运行和完成。
并行是指多个任务或唯一任务的多个部分在逻辑上同时运行的情况。
请记住,并发和并行不是一回事。让我们更详细地了解当我说“并发与并行”的意思。
三、并发
当我们谈论至少两个或更多任务时,并发这个定义是适用的。当一个应用程序实际上可以同时执行两个任务时,我们将其称为并发应用程序。尽管这里的任务看起来像是同时运行的,但实际上它们可能不一样。它们利用操作系统的CPU时间分片功能,其中每个任务运行其任务的一部分,然后进入等待状态。当第一个任务处于等待状态时,会将CPU分配给第二个任务以完成其一部分任务。
操作系统根据任务的优先级分配CPU和其他计算资源,例如内存;依次处理所有任务,并给他们完成任务的机会。对于最终结果来看,用户感觉所有任务都是同时运行的,这称为并发。
四、并行
并行不需要两个任务存在。通过为每个任务或子任务分配一个内核,它实际上使用多核CPU基础结构同时运行部分任务或多个任务。
并行性本质上要求具有多个处理单元的硬件。在单核CPU中,您可能会获得并发性,但不能获得并行性。
并发是由独立执行的进程组成,而并行性是同时执行(相关的)计算。
并发就是一次处理很多事情。并行是关于一次做很多事情。
五、应用程序如何体现并发和并行
一个应用程序可以是并发的,但不能是并行的,这意味着它可以同时处理多个任务,但是没有两个任务可以同时执行。
一个应用程序可以是并行的,但不能是并发的,这意味着它可以同时处理多核CPU中一个任务的多个子任务。
一个应用程序既不能是并行的,也不能是并发的,这意味着它一次顺序地处理所有任务。
一个应用程序可以是并行的,也可以是并发的,这意味着它可以同时在多核CPU中同时处理多个任务。
这就是并发与并行的关系,这是Java多线程概念中非常重要的概念。
学习愉快!
Concurrency vs. Parallelism.
Concurrency means multiple tasks which start, run, and complete in overlapping time periods, in no specific order.Parallelism is when multiple tasks OR several part of a unique task literally run at the same time, e.g. on a multi-core processor. Remember that Concurrency and parallelism are NOT the same thing.
Let’s understand more in detail that what I mean when I say Concurrency vs. Parallelism.
Concurrency
Concurrency is essentially applicable when we talk about minimum two tasks or more. When an application is capable of executing two tasks virtually at same time, we call it concurrent application. Though here tasks run looks like simultaneously, but essentially they MAY not. They take advantage of CPU time-slicing feature of operating system where each task run part of its task and then go to waiting state. When first task is in waiting state, CPU is assigned to second task to complete it’s part of task.
Operating system based on priority of tasks, thus, assigns CPU and other computing resources e.g. memory; turn by turn to all tasks and give them chance to complete. To end user, it seems that all tasks are running in parallel. This is called concurrency.
Parallelism
Parallelism does not require two tasks to exist. It literally physically run parts of tasks OR multiple tasks, at the same time using multi-core infrastructure of CPU, by assigning one core to each task or sub-task.
Parallelism requires hardware with multiple processing units, essentially. In single core CPU, you may get concurrency but NOT parallelism.
Differences between concurrency vs. parallelism
Now let’s list down remarkable differences between concurrency and parallelism.
Concurrency is when two tasks can start, run, and complete in overlapping time periods. Parallelism is when tasks literally run at the same time, eg. on a multi-core processor.
Concurrency is the composition of independently executing processes, while parallelism is the simultaneous execution of (possibly related) computations.
Concurrency is about dealing with lots of things at once. Parallelism is about doing lots of things at once.
An application can be concurrent – but not parallel, which means that it processes more than one task at the same time, but no two tasks are executing at same time instant.
An application can be parallel – but not concurrent, which means that it processes multiple sub-tasks of a task in multi-core CPU at same time.
An application can be neither parallel – nor concurrent, which means that it processes all tasks one at a time, sequentially.
An application can be both parallel – and concurrent, which means that it processes multiple tasks concurrently in multi-core CPU at same time .
That’s all about Concurrency vs. Parallelism, a very important concept in java multi-threading concepts.
Happy Learning !!
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。