併發和並行的區別

前言

看了不少文章都沒有講好這個問題, 一會一個火車的例子, 一會一個白菜的例子,看了也不知所云。找到一篇從CPU的角度講這個問題的文章,抄下來了。
https://howtodoinjava.com/java/multi-threading/concurrency-vs-parallelism/java

併發和並行

併發是指多個任務,這些任務在重疊的時間段內以無特定順序啓動,運行和完成。並行是指多個任務或惟一任務的多個部分在邏輯上同時運行的狀況,例如在多核處理器上。請記住,併發和並行性不是一回事。讓咱們更詳細地瞭解當我說「併發與並行」的意思。

併發

  當咱們談論至少兩個或更多任務時,併發這個定義是適用的。當一個應用程序實際上能夠同時執行兩個任務時,咱們將其稱爲併發應用程序。儘管這裏的任務看起來像是同時運行的,但實際上它們可能不同。它們利用操做系統的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.app

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.ui

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.ip

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 !!

相關文章
相關標籤/搜索