What is the difference between concurrency and parallelism?

前端 未结 30 2666
清歌不尽
清歌不尽 2020-11-22 00:21

What is the difference between concurrency and parallelism?

Examples are appreciated.

30条回答
  •  礼貌的吻别
    2020-11-22 01:00

    Say you have a program that has two threads. The program can run in two ways:

    Concurrency                 Concurrency + parallelism
    (Single-Core CPU)           (Multi-Core CPU)
     ___                         ___ ___
    |th1|                       |th1|th2|
    |   |                       |   |___|
    |___|___                    |   |___
        |th2|                   |___|th2|
     ___|___|                    ___|___|
    |th1|                       |th1|
    |___|___                    |   |___
        |th2|                   |   |th2|
    

    In both cases we have concurrency from the mere fact that we have more than one thread running.

    If we ran this program on a computer with a single CPU core, the OS would be switching between the two threads, allowing one thread to run at a time.

    If we ran this program on a computer with a multi-core CPU then we would be able to run the two threads in parallel - side by side at the exact same time.

提交回复
热议问题