It would be useful to understand the theoretical difference between Await
and async
. These are known as blocking and non-blocking respectively.
Blocking
A blocking computation is much like a while
loop.
while(!done) {
if (isDone) {
// do whatever
done = true
}
}
This is synchronous blocking computation. The Thread
can't do anything else until the blocking action is complete as its constantly checking for the action.
You only really have as many threads as processors on the machine. Hopefully you can see how they can quickly be fully occupied and a giant FIFO queue of "things to do" is formed.
Non-blocking
In essence, the concept is very simple. Instead of continuously checking if something is done, the check is done at given time intervals. The Future
will be checked for completion every 100ms(let`s say).
The awesome thing is during every one of those 100ms breaks, the Thread is free to do something else. That's why you get superior performance from async
things.
Bottom line
It's physically possible for the processors to do more things in a given time interval. It's a very simple syllogism.
Say you and your friend arrange to meet for coffee at 3PM. He doesn't show up.
You can either sit in the coffee shop relentlessly cursing or you can go home and bake cookies. While the cookies are baking, you check your phone every 5 minutes until he eventually texts and then you meet up.
In scenario 1, you are angry and haven't done much all day.
In scenario 2, you are delighted by your culinary success and in a great mood to see a friend.