What is the difference between concurrency and parallelism?

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

What is the difference between concurrency and parallelism?

Examples are appreciated.

相关标签:
30条回答
  • 2020-11-22 00:59

    I really like Paul Butcher's answer to this question (he's the writer of Seven Concurrency Models in Seven Weeks):

    Although they’re often confused, parallelism and concurrency are different things. Concurrency is an aspect of the problem domain—your code needs to handle multiple simultaneous (or near simultaneous) events. Parallelism, by contrast, is an aspect of the solution domain—you want to make your program run faster by processing different portions of the problem in parallel. Some approaches are applicable to concurrency, some to parallelism, and some to both. Understand which you’re faced with and choose the right tool for the job.

    0 讨论(0)
  • 2020-11-22 00:59

    Parallelism: Having multiple threads do similar task which are independent of each other in terms of data and resource that they require to do so. Eg: Google crawler can spawn thousands of threads and each thread can do it's task independently.

    Concurrency: Concurrency comes into picture when you have shared data, shared resource among the threads. In a transactional system this means you have to synchronize the critical section of the code using some techniques like Locks, semaphores, etc.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-22 01:00

    Concurrency => When multiple tasks are performed in overlapping time periods with shared resources (potentially maximizing the resources utilization).

    Parallel => when single task is divided into multiple simple independent sub-tasks which can be performed simultaneously.

    0 讨论(0)
  • 2020-11-22 01:01

    Concurrency is when two or more tasks can start, run, and complete in overlapping time periods. It doesn't necessarily mean they'll ever both be running at the same instant. For example, multitasking on a single-core machine.

    Parallelism is when tasks literally run at the same time, e.g., on a multicore processor.


    Quoting Sun's Multithreaded Programming Guide:

    • Concurrency: A condition that exists when at least two threads are making progress. A more generalized form of parallelism that can include time-slicing as a form of virtual parallelism.

    • Parallelism: A condition that arises when at least two threads are executing simultaneously.

    0 讨论(0)
  • 2020-11-22 01:01

    They solve different problems. Concurrency solves the problem of having scarce CPU resources and many tasks. So, you create threads or independent paths of execution through code in order to share time on the scarce resource. Up until recently, concurrency has dominated the discussion because of CPU availability.

    Parallelism solves the problem of finding enough tasks and appropriate tasks (ones that can be split apart correctly) and distributing them over plentiful CPU resources. Parallelism has always been around of course, but it's coming to the forefront because multi-core processors are so cheap.

    0 讨论(0)
提交回复
热议问题