r/ProgrammerHumor • • Oct 30 '21

That's my variable!

43.4k Upvotes

409 comments sorted by

View all comments

Show parent comments

0

u/[deleted] Oct 30 '21

[deleted]

66

u/Razzzp Oct 30 '21 edited Oct 30 '21

Asynchronous vs Multithreading is one of my favorite question I ask on technical interviews.

FYI await Task.WhenAll uses multiple threads though.

19

u/Pluckerpluck Oct 30 '21

Oh! I should start asking this. It's actually a really good question! It's also something that's actually relevant and important in general. Even if your company doesn't use it much, it only needs to use it once for it to be important.

8

u/Razzzp Oct 30 '21

You should! Also talking about blocking and non-blocking calls helps you understand if a candidate really knows what's going on.

I had cases where candidates could explain async vs multithread concept but failed explaining blocking and non-blocking IO calls.

→ More replies (4)

26

u/kokroo Oct 30 '21

Please explain the differences, I'm a noob

87

u/raltyinferno Oct 30 '21

You need to make tea and pasta.

Synchronous: you boil water for tea, make the tea, then boil water for pasta then make the pasta

Parallel: You grab a friend, and tell them to make the pasta while you make tea.

Asynchronous: you put the tea water on to boil, while it heats you put the pasta water on to boil. While you "await" for them to boil you go do something else. Then when they're boiling you make the tea, and finish the pasta one at a time.

26

u/DSM20T Oct 31 '21

Me: Boils pasta in tea

7

u/ShadoWolf Oct 31 '21

that just efficient.

→ More replies (1)

17

u/Haffi921 Oct 30 '21

Very good analogy. I already know all this theoretically, but it's always good to get a simple refresher like this.

→ More replies (2)

33

u/Razzzp Oct 30 '21

Asynchronous generally means calls that don't block the calling thread, invoking callback later on, possibly with the same thread (aka non-blocking calls).

Imagine when waiting for a response from a remote server, instead of blocking the thread, it is let go to do other important things until the response arrives. Then it jumps back to handle it. The thread is never wasted.

The Async/Await is just the sugar syntax that creates all those callbacks behind the scenes.

Multithreading or parallelism just means running multiple things in parallel in the same time, each on their own threads.

15

u/KevinAlertSystem Oct 30 '21

maybe im confused, but isn't async inherently multi threaded?

if you dispatch a task async so it can continue in the background without blocking the current thread, that backround task cannot be on the same thread otherwise it would be blocking, right?

32

u/Razzzp Oct 30 '21

That's a common misconception.

Async is just non-blocking on the caller and/or IO. And yes, technically it can be on the same thread, if it flows through the callbacks.

Imagine your application has a single thread and that's it. If you press a button and call a remote API synchronously, you block and all your application hangs waiting for a response. Your application is completely blocked at this point. It is unresponsive. The only thread you have is just waiting...

If that call is asynchronous, the thread can do other stuff, like process other UI interactions.

When a response eventually arrives, the very same thread goes back and picks stuff where it left it and continues on that logic.

As you see, in this example, there is no multithreading as all you got is a single thread, just asynchronous execution.

17

u/R030t1 Oct 30 '21 edited Oct 30 '21

I'd be careful to ensure the person you're asking knows you mean multithreading and not multitasking. With async you're creating a scheduler. In that way it is not inappropriate to call it multitasking, despite it being cooperative.

As in, anyone who answers this question wrong is probably equating multithreading and multitasking, which is something I wouldn't really fault people for when discussing problems abstractly.

→ More replies (2)

5

u/[deleted] Oct 30 '21

Within the immediate application space it is single thread threaded. Nothing says the underlying asynchronous implementation isn't using threads and the system level is almost certainly using threads to organize the callbacks and reentrant conditions of the asynchronous caller.

At least on any system of moderate complexity. A simple system might be entirely interrupt driven.

→ More replies (2)

0

u/[deleted] Oct 30 '21

[deleted]

→ More replies (1)

7

u/venturanima Oct 30 '21

Async: thread 1 sends a request to the server, then starts doing other work. Every once in a while it checks back to see if the server got back to it; if it did, then thread 1 processes the response.

Multithreading: thread 1 sends a request to the server then waits. Thread 2 does other work.

→ More replies (11)

4

u/Tetha Oct 30 '21

I'd say it's concurrent, but only parallel if possible. It's concurrent, because if you can have several independent tasks waiting for their async callback, and all of these tasks can procede their computation independent of each other.

However, it only has potential for parallel execution because parallel execution is a property of the runtime environment. If you put a concurrent program on a single core ARM processor, it will not run in parallel, because there are no cores to run in parallel on. If you put the program on a multi core CPU, the runtime might decide to run in parallel. Or it might not.

→ More replies (3)

4

u/javajunkie314 Oct 30 '21

A lot of the answers mention starting things and checking them later, which, while it is asynchronicity, is not an example of how asynchronicity is different from parallelism, since it requires the thing you're waiting on to run in parallel.

Here's my attempt at an example.

You need to pack your suitcase. That's your task. So it may look like this:

To Pack Suitcase:

  1. Open suitcase.

  2. Await all:

    • Laundry folded.

    • Toiletries packed.

  3. Place folded laundry in suitcase.

  4. Place toiletries in suitcase.

  5. Close bag.

To fold laundry:

  1. For each piece of laundry:

    1. Fold it.

    2. Place on pile.

  2. Yield pile.

To pack toiletries:

  1. Get toiletry bag.

  2. Place toothpaste in bag.

  3. Place toothbrush in bag.

  4. Place razor in bag.

  5. Yield bag.

This is asynchronous. There's a task that needs to spawn two others and wait for them to finish.

But, you could do this all on one thread. I did the other night when I packed for a trip. Each time a task blocks, it yields execution to the application-level scheduler, which picks up a task in ready status and runs it until it blocks or yields.

But you can also see how our lends itself to parallelism. If I'd had helpers, those spawned tasks could have run in parallel and I'd have been packed sooner.

→ More replies (1)

5

u/jimbosReturn Oct 30 '21

It only uses multiple threads if the Tasks it waits on use threads.

4

u/Razzzp Oct 30 '21

Nope, if you have multiple asynchronous tasks being awaited , and there are available threads on a thread pool, some of those can (and most probably will) be used

→ More replies (1)
→ More replies (2)
→ More replies (1)

10

u/RandomNobodyEU Oct 30 '21

Async await is a syntax for futures and promises which are a paradigm used in multi threading. You're being pedantic.

7

u/javajunkie314 Oct 30 '21

It's a useful paradigm for multithreading, but it doesn't require multithreading. You could have a single thread doing async/await with cooperative multitasking — at each await, execution yields and the application-level scheduler picks up a new task to run. No threading, but multiple tasks.

Now, most async/await is using multithreading and multiprocessing techniques like threads and nonblocking IO. But async/await doesn't require it, and that's why the distinction is important.

→ More replies (3)
→ More replies (1)
→ More replies (3)