Concurrency vs Parallelism


You are probably reading this post as you are familiar with multithreading and multiprocessing but having a feeling of being somewhat confused with the concurrent and parallel way of execution.

Basically, Concurrency and Parallelism are related to the way an application executes. It is a parameter that is taken into consideration while designing various applications.

So, let us dive deep into this and see what these terms are and what is the exact difference between them.

Concurrency

When an application is dealing with multiple tasks at the same time i.e. it makes progress on more than one task, it is said to be concurrent.

Still confused, right??

Dealing with multiple tasks means that if an application has only one processor for execution, the processor will not be able to process multiple tasks at the same time. In this case, the scheduler kicks in and schedules the tasks such that the processor makes progress on one task, and then context switches to another task and makes progress on it. The scheduling of these tasks is based on many parameters but one of the major parameters is time-sharing/slicing.

For example, people standing in two queues to access one vending machine. In this case, one person alternatively from each queue will access the vending machine at a time.

Concurrency gives a feel of parallelism. The main goal of concurrency is making the application more useful than making it faster. For example, a background thread must not affect the performance of a thread which is taking care of user experience.

Parallelism

When an application actually makes progress on multiple tasks at the same time, it is called parallelism. Here the application splits its tasks into smaller subtasks which can be processed in parallel on different CPUs at the same time. Thus, it is a prerequisite to have multiple processors in order to achieve parallelism.

For example, people standing in two queues to access two different vending machines. In this case, one person from both the queues will access both the vending machines at a time.


The main goal of parallelism is to have fast execution of tasks. The tasks should be divided such that they should not communicate with each other and should be isolated. This makes the process faster in execution. If the tasks are meant to communicate with each other, use concurrency.


Concurrency vs Parallelism


Concurrency
Parallelism

Dealing with lots of things at the same time
Doing lots of things at the same time
This can be achieved with one CPU
This cannot be achieved with one CPU. It needs multiple CPUs
This is deterministic as the sequence of execution may vary
This is deterministic as the sequence of execution does not vary
The goal is to make the application more useable
The goal is to make the application faster.
The tasks communicate with each other and share resources
The tasks should not communicate with each other in order to achieve parallelism.
This is mainly used in large scale applications
This is not mainly used in large scale applications
Example: Two queues accessing one vending machine
Example: Two queues accessing two vending machine


Best of both worlds

An application can be both concurrent as well as parallel in which case the tasks are executed concurrently, and also these tasks are divided into isolated subtasks for parallel execution. In this case, there are multiple tasks and multiple processors.

Be that as it may, a portion of the advantages of concurrency and parallelism might be lost in this situation, as the CPUs are already busy with either concurrency or parallelism alone. Combining these two may lead to a small performance gain or even performance loss.

The design decision of an application should be taken very cautiously keeping in mind the goals of these two approaches.


Thanks for reading 😊

Comments

Popular posts from this blog

What happens when you watch stuff on Netflix?

DHCP: Dynamic Host Configuration Protocol