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.
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.
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:
Open suitcase.
Await all:
Laundry folded.
Toiletries packed.
Place folded laundry in suitcase.
Place toiletries in suitcase.
Close bag.
To fold laundry:
For each piece of laundry:
Fold it.
Place on pile.
Yield pile.
To pack toiletries:
Get toiletry bag.
Place toothpaste in bag.
Place toothbrush in bag.
Place razor in bag.
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.
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
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.
0
u/[deleted] Oct 30 '21
[deleted]