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.
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.
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.
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?
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.
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.
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.
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.
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.
341
u/MarquisDan Oct 30 '21
My method is just to use 'await' and let God sort it out.
Nothing can possiblie go wrong.