Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

whats the difference between asynchronous and multithreading

whats the difference between asynchronous and multithreading

You just have to look at the word "asynchronous". Synchronous means that all of the things are done at once. So Asynchronous, as its opposite, means that everything is not done at once, but may occur at any time, rather than in a specific order.

Multithreading is asynchronous, because each Thread is working independently, and may call for a certain response from the main thread at any time.

Threading is about 'workers', asynchrony is about tasks. 
A greate example I have read somewhere else: 

Synchronous: you 'cook' the eggs then you 'cook' the toast

Asynchronous single threaded: you start the eggs cooking and set a timer. You start the toast cooking, and set a timer. While they are both cooking, you clean the kitchen. When the timers go off you take the eggs off the heat and the toast out of the toaster and serve them.

Asynchronous multi threaded: you hire two more cooks, one to cook eggs and one to cook toast. Now you have the problem of coordinating the cooks so that they do not conflict with each other in the kitchen when sharing resources. And you have to pay them.

"synchronous" means that you block on the call until it finishes executing. Asynchronous means you do not block. You can't say that synchronous means "at once" because that implies that everything is happening simultaneously. If I fire off 5 web requests synchronously, then they each happen one after another, and I don't continue on with other code until all 5 return--again, one after another. If I make those same 5 requests asynchronously, then they most certainly can happen at the same time. Now, those may not finish at the same time, but they (more than likely) are being processed at the same time. I can even continue on with other code, and expect those 5 results at a later time should I so desire. That's the beauty of async/await.

In the async/await world, we are typically using async to avoid blocking on I/O logic. When the hardware is busy doing stuff outside of the CPUs purview, then the CPU accomplishes nothing meaningful while it sits there waiting for I/O to complete. We use async code to allow the CPU to continue on with meaningful work while the hardware finishes its tasks. When the hardware is done, it signals the CPU using an I/O completion port (or crudely, the hardware signals the CPU/raises an event with the CPU).

Also, you cannot say that multi-threading is asynchronous. It can be. I can kick off a thread to make one of those web requests I mentioned above. But creating a thread incurs a bit of overhead. You can certainly have synchronous threads. You can force one thread to wait until some other thread reaches some arbitrary point. Now you have to do this using shared resources, which must be protected from corruption. This protection is part of what makes multi-threading code so difficult.

This post first appeared on Asp.netSourceCodes, please read the originial post: here

Share the post

whats the difference between asynchronous and multithreading


Subscribe to Asp.netsourcecodes

Get updates delivered right to your inbox!

Thank you for your subscription