DEV Community

Concurrency vs. Parallel vs. Async in .NET

Scott Hannen on April 16, 2019

I read a few posts that made me question whether I understood these concepts or could explain them clearly. There were lots of diagrams, and at l...
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

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 • 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.

Collapse
 
chinhp profile image
ChrisP • Edited

Let me summary them in a story

  • Bob started a restaurant and he does all the thing: Being a chef, being a waiter and cashier. this kind of system is non concurrency
  • More and more customers come. Bob decided to hire 1 Chef, 1 Waiter and 1 Cashier. He just enjoys to do the management things. Now, at the same time, Waiter gets order while Cashier collects payment from another customer: Now we have a *concurrency * system
  • The number of customer continues being increase. 1 waiter is not enough. Bob decided to get another waiter. Waiter 1 gets order from customer table number 1 to 10, waiter 2 gets order from table number 11 to 20. Getting order is now divided between 2 waiter. Now we have *Parallel * system.
  • Waiter 1 after getting the order, he bring the order to the kitchen and wait for the food. He just standing there and keep waiting for the Chef. After the food is ready, he bring the food to his customer and getting next order. Bob doesn't want to do this way, he ask waiter 1 stop waiting, After the food is ready, the chef will inform and either waiter 1 or waiter 2 can bring it to the customer. Now we have *Async *
Collapse
 
sayeduzzamancuet profile image
SAYED UZ ZAMAN • Edited

Thanks for amazing explanation, the comments are even better.
Developer always tends to make confusion between multi threading and parralel task execution. Lets say, you have two articles to write.
In multi threading fashion,
You will complete half of para-1, then switch to para-2 and do half of para-2 and bla bla..
But in parralel fashion,
You will use your both hand two write both paragraphs at the same time. So these two paragraphs should not be related/depends on each other.
And, I am not sure, probably if you wanted to write a paragraph as para-1, and paragraph summery as para-2 then you have to ensure para-1 finishes first so that based on this you can do the next task. This should be async fashion, isn't it?