DEV Community

Discussion on: Concurrency vs. Parallel vs. Async in .NET

Collapse
rhymes profile image
rhymes • Edited

I think your definition of concurrency is actually parallelism. If the system is doing two things at the same time, then it's doing them in parallel.

The subtle difference is that concurrency means that the system is able to advance multiple tasks indipendently, parallelism is that it's able to advance them at the same exact time.

Love this definition by Rob Pike's famous Concurrency is not parallelism slides:

Concurrency is about dealing with lots of things at once.
Parallelism is about doing lots of things at once.

You can see concurrency as a round table with 5 people sitting on it and a waiter. The waiter is able to mind each person's request indipendently (hence advancing the tasks) in a few minutes but he's not able to talk to two persons at the same time. To achieve that you need at least one more waiter, so that one talks to a person and another talks to another person at the same time.

Async is a programming model. It can be implemented without threads, I believe .NET implements with threading, Node.js for example uses a single thread with an event loop to achieve async.

This is always a tricky topic because we tend to conflate concurrency models with the definition of concurrency and parallelism.

Hope it clears up things a bit.

Collapse
scotthannen profile image
Scott Hannen Author

What I'm beginning to realize is that, as if this wasn't confusing enough, the common use of these terms in the scope of .NET programming is not quite the same as their common meaning. That's unfortunate because using the same terms to describe different things undermines the reason why we have terms.

Specifically, most scenarios involving concurrency in a .NET application involve multiple threads. In the broader sense concurrency does not require multiple threads, but the purpose of all the classes in the System.Collections.Concurrent namespace (like ConcurrentQueue) is to support multiple concurrent threads.

When a website handles multiple requests and uses multiple threads, that is both concurrent and parallel. The use of multiple threads really fits the definition of "parallel," but if you google ".net parallel web requests" the results all describe distributing the work of making requests, not receiving them.

I qualified the post as being .NET-specific, but this means I have to qualify it even more.

Collapse
scotthannen profile image
Scott Hannen Author • Edited

I think where this gets mixed up is that concurrency involves any tasks being executed at the same time. That's the literal meaning of concurrency.

Parallelism involves taking a specific set of tasks and executing them across multiple threads rather than sequentially.

That lines up with the sentence just before the one you quoted:

In programming, concurrency is the composition of independently executing processes, while parallelism is the simultaneous execution of (possibly related) computations.

In other words, concurrency means that multiple things are going on the same time (the literal meaning of "concurrent.") One user is submitting an order. Another submits an order a second later while the previous one is still processing. Another is updating profile information. All are happening at the same time.

Parallelism relates to breaking up a task into separate parts and executing them simultaneously. Perhaps a calculation can be separated into multiple calculations. There may be a number of nearly identical calculations.

I can see where there's room for confusion. Both concurrency and parallelism result in things happening at the same time on multiple threads. We could say that concurrency is incidental. We can process two requests at once, but if we get them one at a time then we only process them one at a time.

Parallelism is deliberate. I've got to do a bunch of stuff so I'm going to distribute the load across multiple threads.

That's why I separated async from the other two. While the terms have distinct meanings, in practice there's some overlap. But async is not about multiple threads.