Parallelism vs Concurrency
I’m not ashamed to admit that, up until “not too long ago”, I struggled with differentiating parallelism from concurrency in the context of computer programs. Don’t get me wrong, I wasn’t completely oblivious to the concepts - I understood them to some extent. I just couldn’t explain or differentiate them to a five-year-old or a recruiter in a technical interview.
I was reading a stack overflow comment(not to be confused with an answer) where the user explained, in very few words, the differences between the two concepts in the simplest way imaginable (at least IMO). It was a Eureka moment for me.
TL;DR: here’s the link to the original StackOverflow question, and below is the comment (adding it here because there’s no way to get the link to a StackOverflow comment):
short answer: Concurrency is two lines of customers ordering from a single cashier (lines take turns ordering); Parallelism is two lines of customers ordering from two cashiers (each line gets its own cashier).
In the following paragraphs, I’ll explain the mental model I used to finally get the hang of parallelism & concurrency.
Parallelism could be likened to two straight, parallel lines where each line consists of a customer care rep & some customers in a queue. In this case, each customer care rep is serving their own line(queue) of customers simultaneously. So, customers are attended to at the same time (in parallel) by the representative in front of their line.
Concurrency, on the other hand, is like two straight lines of customers in a queue but with just one customer care rep attending to them. This single rep manages both queues by switching between them, focusing on one queue at a time.
What makes it so easy for me to put the pieces together using this mental model is thinking of processes as customers in a queue, execution as each customer getting served, and the list(usually a queue) of processes as the queue of customers. The customer care reps here represent the CPU cores or threads handling the processes (or tasks).
To remember which definition is which, I just need to remember a simple math rule which states that parallel lines will never meet :).
So, in summary, Concurrency is like a single-core processor managing multiple tasks by switching between them, while Parallelism is like a multi-core processor executing multiple tasks at the same time, in parallel.
References
- What is the difference between concurrency and parallelism? on StackOverflow
- User @ccharvey on StackOverflow