Understand: Concurrency vs. Parallelism

Understand: Concurrency vs. Parallelism

The terms concurrency and parallelism are often used in relation to multithreaded programs.

Introduction

Hi folks. Recently, I appeared for a backend developer interview. I got an interesting question from an interviewer What are Concurrency and Parallelism & How are they related to synchronous and asynchronous execution?

So let us check whether you have understood these terms on sowftware engineering in the right way. We would analyze and understand what they are and their relationship with one another.

Concurrency & Parallelism

Concurrency means that an application is making progress on more than one task at the same time (concurrently).

Parallelism means that an application splits its tasks up into smaller subtasks that can be processed in parallel, for instance on multiple CPUs at the exact same time.

Concurrency

Consider you are given the task of singing and eating at the same time. At a given instance of time either you would sing or you would eat as in both cases your mouth is involved. So in order to do this, you would eat for some time and then sing and repeat this until your food is finished or the song is over. So you performed your tasks concurrently.

Concurrency means executing multiple tasks at the same time but not necessarily simultaneously.

Parallelism

Consider you are given two tasks cooking and speaking to your friend over the phone. You could do these two things simultaneously. You could cook as well as speak over the phone. Now you are doing your tasks parallelly.

Parallelism means performing two or more tasks simultaneously.

Synchronous and Asynchronous

Synchronous

Imagine you were given to write two letters one to your mom and another to your best friend. You can not at the same time write two letters at the same time.

In a synchronous programming model, tasks are executed one after another. Each task waits for any previous task to complete and then gets executed.

Asynchronous

Imagine you were given to make a sandwich and wash your clothes in a washing machine. You could put your clothes in the washing machine and without waiting for it to be done, you could go and make the sandwich. Here you performed these two tasks asynchronously.

In an asynchronous programming model, when one task gets executed, you can switch to a different task without waiting for the previous one to get completed.

  • Concurrency and Parallelism refer to computer architectures that focus on how our tasks or computations are performed.

  • In a single core environment, concurrency happens with tasks executing over the same time period via context switching at a particular time period, only a single task gets executed.

  • In a multi-core environment, concurrency can be achieved via parallelism in which multiple tasks are executed simultaneously.

References

https://softwareengineering.stackexchange.com/questions/190719/the-difference-between-concurrent-and-parallel-execution

https://youtu.be/TwnWSfNFVnI?feature=shared

https://youtu.be/ENc_sLdUlFE?feature=shared

https://medium.com/swift-india/concurrency-parallelism-threads-processes-async-and-sync-related-39fd951bc61d

Conclusion

Distinguishing parallelism from concurrency is important to seek a fitting way to solve large-scale problems, but they are considered interchangeable in reality.

You can reach me for any query, or feedback or just want to have a discussion. Share your thoughts & comments in the comments section to make me understand to the core.

Did you find this article valuable?

Support Saravana sai blog by becoming a sponsor. Any amount is appreciated!