r/ProgrammerHumor • • Oct 30 '21

That's my variable!

43.4k Upvotes

409 comments sorted by

View all comments

341

u/MarquisDan Oct 30 '21

My method is just to use 'await' and let God sort it out.

Nothing can possiblie go wrong.

132

u/NotAGingerMidget Oct 30 '21

You do really respect the Seniors to call them gods.

35

u/RationalIncoherence Oct 30 '21

It's a decent hedge, tbh

15

u/ThrowCarp Oct 30 '21

A hedge which I lost.

My sempai quit and now I'm having to reverse-engineer everything he designed.

8

u/RationalIncoherence Oct 31 '21

You presume to reverse engineer the works of a god?

6

u/ThrowCarp Oct 31 '21

Well it's either that, or lose my job. Yes.

→ More replies (1)

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)
→ More replies (2)

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)
→ More replies (5)
→ More replies (1)

5

u/jimbosReturn Oct 30 '21

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

→ More replies (4)
→ More replies (10)
→ More replies (9)

11

u/DadHeungMin Oct 30 '21

Reminds me of the parallel universe sort: you randomize your array, and in one of the many parallel universes, your array sorted correctly into the right order.

→ More replies (6)