The document discusses asynchronous and parallel programming approaches. Asynchronous programming allows non-blocking execution so that code can continue running without waiting for long tasks to complete. Parallel programming divides tasks into independent pieces that can be processed simultaneously across multiple CPU cores or machines to finish work more efficiently. Both approaches aim to improve performance by reducing wait times and better utilizing available resources.
2. “
”
Asynchronous or Parallel programming is not
something that you should spend a lot of time thinking
about.
In developers eye, these are two approaches to solve following basic
problems:
User Responsiveness along with wait time utilization
Resource utilization
4. 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.
5. 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.
7. 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.
8. Asynchronous Programming
• 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.
9. Asynchronous Programming
• 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.
10. Parallel Programming
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 the main task faster? This is what called Parallelism.
• In this case there must be available/free resources to do the job in parallel.
• These resources could be cores of a CPU or even multiple machines.
11. Parallel Programming
• There must be multiple threads otherwise how can job works get
processed/done in parallel.
• In this case programmer needs to have more control over threads/cores of
CPUs in precise manner.
12. Things without technical terms
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.