Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between Multithreading and Async program in c#

I have initially searched in Stackoverflow and google for a similar type of question. Only one link gave some points, but I can't understand clearly. [1]

The following questions haunts me:

  1. In Asynchronous programming, what is real Callback method? is delegate?

  2. The implent of async program is using multi threads?

If provided with graphics to explain, I would be very grateful


[1] "Difference between Multithreading and Asynchronous programming"

like image 420
yubaolee Avatar asked Apr 17 '15 10:04

yubaolee


2 Answers

Single Threaded Blocking

To understand asynchronous/concurrent/multi-threaded we need to start with the most basics and WHY we add so much complexity to the issue.

In the beginning there was only really Single Threaded Blocking applications. These are really simple programs, and more than likely, these are what you are writing right now.

To explain, I will use an analogy of a Pizza House.

Imagine your computer is a pizza house with a single employee. He has only had basic training.

You walk into the shop, talk with the employee, look at the menu and order a pizza.

Great. The employee walks into the back of the shop, puts a pizza in the over and stands next to the oven waiting for the pizza to cook.

You remember your wife doesn't like pineapples. You shout at the employee trying to get his attention to change your order. No dice. He will not budge from the oven (he fell asleep).

You get annoyed and leave.

Multi-threaded Concurrent Blocking

You go to the next pizza shop.

It has 4 employees.

You walk into the shop, talk with the employee, look at the menu and order a pizza.

He shouts (sends a message) to the other employees in the back to make you a pizza. They put one in the oven. He stands next to the oven, and sleeps.

You forget your wife has allergies to bacon. You tell employee A to cancel the pizza. He yells inside to the cook, wakes him up. The cook throws the pizza in the bin and puts a Kosher pizza in the oven. He falls back asleep promptly.

You wait, pizza is ready, you get a bill. Its massive (hiring too many staff, and half of them sleep on the job).

Single Threaded Asynchronous/Non-blocking

You go to the next pizza shop.

It has 1 employee.

You walk into the shop, talk with the employee, look at the menu and order a pizza.

He walks inside, puts a pizza in the oven. He then attaches the receipt (callback) to the pizza. He walks back out to the counter.

You remember your wife actually doesn't eat meat this month. You tell the employee, he goes inside, fixes the situation.

After the employee checks inside for a done pizza. He then reads the receipt (give this pizza to Bob).

You get cheap affordable and responsive pizza.

like image 115
Aron Avatar answered Nov 08 '22 22:11

Aron


The difference between the multi threading and async programming is that in the multithreading we create a new thread for a function to perform or complete only this function or task

async programming also use multithreading but in the different way that is in async a work or task is divided into multiple thread

for example if we have 4 tasks if we use multithreading and assign the threads as follows

Thread1 = task1
Thread2 = task2
Thread3 = task3
Thread4 = task4

but when we use async programming model we not have to assign the threads

task are auto divided among threads may be it use single thread or multiple thread as it need

if it will use single thread then task 1-4 will work concurrently

in round robin fashion

the CPU switch among task , start a task run it for few seconds , and then save its position 'context switching' then it start other task . this happen so rapidly and seem like illusion that all tasks are running at the same time

similarly when we have multiple threads in async model

a single task is handle by multiple thread e.g if task1 is started by Thread1 , it may be run on thread2 or thread3 when thread1 is not working on task1

This is the beauty of async programming

i think its the best explanation for the beginners

https://codewala.net/2015/07/29/concurrency-vs-multi-threading-vs-asynchronous-programming-explained/#comment-21276

like image 35
Basit Avatar answered Nov 08 '22 22:11

Basit