What is the difference between concurrency and parallelism?

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

What is the difference between concurrency and parallelism?

Examples are appreciated.

30条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2020-11-22 01:24

    "Concurrency" is when there are multiple things in progress.

    "Parallelism" is when concurrent things are progressing at the same time.


    Examples of concurrency without parallelism:

    • Multiple threads on a single core.
    • Multiple messages in a Win32 message queue.
    • Multiple SqlDataReaders on a MARS connection.
    • Multiple JavaScript promises in a browser tab.

    Note, however, that the difference between concurrency and parallelism is often a matter of perspective. The above examples are non-parallel from the perspective of (observable effects of) executing your code. But there is instruction-level parallelism even within a single core. There are pieces of hardware doing things in parallel with CPU and then interrupting the CPU when done. GPU could be drawing to screen while you window procedure or event handler is being executed. The DBMS could be traversing B-Trees for the next query while you are still fetching the results of the previous one. Browser could be doing layout or networking while your Promise.resolve() is being executed. Etc, etc...

    So there you go. The world is as messy as always ;)

提交回复
热议问题