Concurrency vs Parallelism - What is the difference? Any examples
concurency: multiple execution flows with the potential cu share resources
Ex: two threads competing for a i/O port.
paralelism: splitting a problem in multiple similar chunks.
Ex: parsing a big file by running two processes on every half of the file.
Concurrency is when two 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. Eg. multitasking on a single-threaded machine.
Parallelism is when tasks literally run at the same time, eg. on a multicore processor.
Quoting Sun's Multithreaded Programming Guide:
Parallelism: A condition that arises when at least two threads are executing simultaneously.
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.
If multiple processes are running in parallel, they are all accomplishing their tasks simultaneously and independently of each other. For example:
Process A Process B
----------- -----------
Step 1 Step 1
Step 2 Step 2
Step 3 Step 3
Step 4 Step 4
However, if multiple processes are running concurrently, they each take turns working toward accomplishing their goals.
Process A Process B
----------- -----------
Step 1
Step 1
Step 2
Step 2
Step 3
Step 3
Step 4
Step 4
Concurrency can often appear like parallelism if the switching between processes is rapid enough.
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.