12
votes

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"

4
Asynchronous does not always imply multi-threaded. ...But a single-threaded asynchronous system will always execute with interleaving, even on a multi-processor system. - Brown University. The difference is that of concurrencyMickyD
There is a good article at codeproject, pls refer the link, codeproject.com/Articles/14931/Asynchronous-Method-InvocationShyju
@Shyju I think this is just what I want,so easy to understand.Thank very much.yubaolee
@MickyDuncan +1 Thank for your editor and your pdfyubaolee
By "async", do you mean the C# async keyword, or a shorthand for "asynchronous"? The two aren't completely equivalent.Ben Voigt

4 Answers

8
votes

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.

3
votes

1) A callback is basically a delegate passed into a procedure which that procedure will "call back" at some appropriate point. For example, in asynchronous calls such as WebRequest.BeginGetResponse or a WCF BeginXxx operation, you'd pass an AsyncCallback. The worker will "call back" whatever method you pass in as the AsyncCallback, in this case when it's finished to let you know that it's finished and to obtain the result.

2) Multithreading is different parts of a program running, typcially called threads.

Asynchronous programming uses threads to kick off a piece of code. So asynchronous programming relies on multithreading to work. Refer to below link:

Async Programming and Multi Threading

Difference between Multithreading & Async Prograaming

1
votes

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

-1
votes
  1. Asynchronous programming is the ability to execute a piece of code parallel to the main program flow. This can be web service calls or any task in current application that is executing. The callback is either a named or anonymous method which can be represented represented by a delegate. The callback may return with a result or exception. Since results from async methods are returned at any point of time, if the results of async methods are queried,the current executing thread will block till the method returns with either exception or result. .Net 3.0 used BackgroundWorker, BeginInvoke/EndInvoke and IAsyncResult.(still being used) .net 4.0 has Tasks .net 4.5 has async await to accomplish this

  2. async calls are impletmented using multithreading. Based on the implemenation, for example if Tasks are used to do some work asynchronously which uses threadpool threads, then the framework will determine if the code will be executed on current thread or it will require new threads.