Asynchronous programming is not something that you spend a lot of time thinking about. Yes, believe me. Just start analysing the requirement in developers eye keeping in mind that you want to develop an resource & time efficient application.
Laying the groundwork - In Developer Eyes
Scenario 1 : As a developer, I want to let continue the execution of code to next line instead of getting stuck or awaiting the completion/result of a long running code-line(eg. network or database operations).
Example Case : A long running execution is happening and instead of await of completion/result, we want to continue further without wasting time.
Scenario 2 : As a developer, I want to finish Task, time-efficiently and utilize other available cores of CPUs or machines by dividing Task into independent chunk.
Example Case : Now a days or CPU has multi cores and at a time, only one core is being used to execute the task. Here, we want to utilize rest cores of CPU by distributing the job.
Mechanism to achieve above 2 scenario/ idea :
Well, now we have 2 valid ideas to boost the performance of application. For the implementation of these ideas we need some special mechanism in coding. And these mechanism are known as Asynchronous & Parallel programming respectively. :) So far easy? Good. :)
Asynchronously means non-blocking i.e. you can go ahead without waiting on a long running line of code. For example- To get done a Task, if you have written 4 lines of code in which 2nd line is time consuming method call(may be related with network I/O or Database call). Now, you want to apply some mechanism here that your 3rd line get started execution without waiting of the completion or result of your 2nd line. This is the non-blocking style.
Coming to second scenario, just think if you can break your Task in independent pieces of work, then? Would not you like to get these independent pieces of work to be done in parallel to finish faster? This is what called Parallelism. In this case there must be available/free resources to do the job in parallel. (resource could be cores of a CPU or even multiple machines.)
Ahh! You must got clear the requirement of these 2 mechanism in programming. So far we have concluded that these two approaches are enabling us to finish our task faster (utilizing the wait-time & free-resource).
Let’s clear the things with more technical terms :)
If we say that Asynchronous is of "no-wait time & continuous execution" approach of coding then this line itself clears us that we are talking about "Single threaded" processing. It also clears that that it is all about to a "concurrent work" fashion.
Concurrent ? : In single threaded process, at the same time, many lines of code can be executed concurrently. To achieve this, Async & Callbacks mechanism is used. It involves heavy Context Switching and Time Scheduling.
* You can conclude at this point that : "An Asynchronous Task might use Parallelism".
In case of Parallel programming, there must be multiple threads otherwise how can that get processed/done in parallel. In this case programmer needs to have more control over threads/cores of CPUs in precise manner.
Asynchronous – Hey you, do this task and come back with results to me. Meanwhile I will get done other tasks. (no wastage of time :)
Parallel – Hey you, do this task and come back with results to me. Meanwhile I will do nothing but waiting you. You can smartly take help of other free resources/cores of CPUs/ machines to boost the work time.
Parallel programming is like working of developers (team) on a project module with a common goal to complete the Task (project module job work). :)
- @AnilAwadh