No history yet

Concurrency and Parallelism

Dealing With Multiple Tasks

In programming, we often need to handle multiple tasks that seem to happen at the same time. Think about a web server juggling requests from thousands of users, or a mobile app downloading data while its user interface remains responsive. Managing this complexity is the job of concurrent and parallel programming.

These two terms, concurrency and parallelism, are often used interchangeably, but they describe different concepts. Understanding the distinction is crucial for writing efficient and scalable applications.

What is Concurrency?

Concurrency is the art of managing multiple tasks at once. It's a way of structuring a program so that different parts can execute independently and out of order, without affecting the final outcome. The tasks might not actually run at the same instant, but they are all in progress during the same period.

Imagine a chef in a kitchen preparing a meal. They have to chop vegetables, simmer a sauce, and bake bread. The chef can't do all these things at the exact same moment. Instead, they switch between them. They'll start the bread, then put the sauce on to simmer, and while both of those are cooking, they'll chop vegetables. The tasks are interleaved. From a high level, all dishes are being prepared at once, but at any given microsecond, the chef is only performing one action. This is concurrency.

In computing, this often happens on a single CPU core. The operating system rapidly switches between different tasks, giving each a small slice of time. Because the switching is so fast, it creates the illusion that the tasks are running simultaneously.

What is Parallelism?

Parallelism is about doing multiple tasks at the same time. This is not an illusion; it's the literal, simultaneous execution of different processes. To achieve parallelism, you need hardware with multiple processing units, like a multi-core CPU.

Let's go back to our kitchen. If the head chef hires two assistants, the kitchen can now operate in parallel. While the head chef chops vegetables, one assistant can stir the sauce and the other can knead the bread. Three distinct tasks are happening at the exact same moment because there are three people to perform them. This is parallelism.

A concurrent program can be run in parallel. If our chef's recipe (the concurrent program) is broken into independent steps, it can be executed by multiple chefs (parallel hardware) to finish faster. But concurrency and parallelism are not the same thing.

Key Differences

The core distinction lies in how tasks are handled. Concurrency is about structure; parallelism is about execution.

Concurrency is about dealing with many things at once, while parallelism is about doing many things at once.

A program can be concurrent without being parallel. Our single chef is a perfect example. They manage multiple tasks concurrently on a single "processor." But to be parallel, a program requires the hardware to support it. You can't have parallelism with only one CPU core.

FeatureConcurrencyParallelism
GoalTo manage and structure access to shared resources.To execute multiple tasks simultaneously to speed up computation.
How it worksTasks are interleaved via context switching on a single core.Tasks run at the same time on multiple cores.
RequiresA single processing unit is sufficient.Multiple processing units are necessary.
AnalogyOne person juggling multiple balls.Multiple people each juggling one ball.

The rise of multi-core processors in everything from servers to smartphones has made parallelism a common tool. However, concurrency is arguably a more fundamental concept. It helps us write cleaner, more manageable code for problems that are inherently about handling many things at once, like user interactions, network requests, and database operations. Often, the goal of concurrency isn't just speed, but also responsiveness.

Quiz Questions 1/5

Which of the following best describes the concept of concurrency?

Quiz Questions 2/5

Imagine a kitchen with one chef who starts boiling water, then begins chopping vegetables while the water heats up. This act of switching between tasks is an example of: