Concurrency vs Asynchronous

http://blog.stevenedouard.com/asynchronousness-vs-concurrency/

Concurrency

There are various different ways of accomplishing concurrency. One of them is parallelism–having multiple CPUs working on the different tasks at the same time. For example, its like having more than 1 chef working on 5 recipes AT THE SAME TIME.

But parallelism is not the only way to achieve concurrency. Another is by task switching, which works like this: Task A works up to a certain point, then the CPU working on it stops and switches over to task B, works on it for a while, and then switches back to task A. If the time slices are small enough, it may appear to the user that both things are being run in parallel, even though they’re actually being processed in serial by a multitasking CPU.

A characteristic of task switching is asynchronous. Asynchronous is when a task DOES NOT BLOCK the worker, and the worker is free to work on other parts of other tasks.

Asynchronous is when a chef is working on 5 dishes at the same time. Going through steps of each recipes one after another WITHOUT IT WAITING (or blocking in IT terms). The chef does not let the recipe step BLOCK HIM from moving on to other recipe tasks.

For example

He preps dish 1, apple pie. then puts it into the oven for a 1 hour bake. Now, the baking process DOES NOT BLOCK him from working on other recipes. So that he does not waste time waiting on the oven, he moves on to other recipe(s).

While dish 1 bakes, he moves on to dish 2, which to chop up some Potatoes and salt some beef chunks. After the potatoes are prepped, he throws them into a pot and starts simmering them with some beef chunks. The simmering process DOES NOT BLOCK HIM from moving on to other recipes. Thus, he is free to move on to other tasks of other recipes.

He then moves on to dish 3, which is to simply toss some salad. He is done with dish 3.

He then goes on to dish 4, which to boil some pasta. Fire on high and the water warms. The boiling process DOES NOT BLOCK him from working on other recipes, so he moves on.

For dish 5, he just chops some fruits and throws yogurt on top. Done!

Dish 4 (al dente pasta) water has now boiled and it DINGS our chef so he knows. He then comes back to Dish 4, salts the water, then throw in pasta. The pasta needs to cook for a few minutes. This cooking process DOES NOT BLOCK him, so he moves on to other dishes.

All Dishes are not processing in some way…so the chef now waits. After a few minutes, Dish 4 (pasta) finishes boiling. It DINGS the chef to let him know. The chef takes the pasta out and finishes preparing for it.

Now, the chef has nothing to do and waits for the next DING.

After an hour, dish 1 finishes baking and DINGS him. Letting him know that dish 1 needs attention. The chef goes to dish 1, takes out the apple pie..puts some vanilla ice cream on the side, and done.

He now has nothing to do and waits. Then after another hour, dish 2 finishes simmering and a timer DINGS him. He then slowly spoons out the morsels, chops some parsley, and finishes dish 2.

So as you can see, asynchronous means that there is one worker, one process, one chef…but its working on multiple tasks.

No task is holding on to him. Whenever he hits a point of a recipe where the recipe needs to take some time (bake, simmer, boil..etc), then he moves on to the next recipe’s task.

In IT term, asynchronous-ness is the ability to NOT stop on a task which depends on an external system (like reading a file from the file system, or loading data from the internet) and continue the processing of the application.