asynchronous and parallel programming

13
Asynchronous and Parallel Programming The basics of these 2 hot programming approaches @AnilAwadh +AnilKumarMVP http://CodePattern.net/

Upload: anil-kumar

Post on 16-Jul-2015

126 views

Category:

Software


1 download

TRANSCRIPT

Page 1: Asynchronous and parallel programming

Asynchronous and

Parallel Programming

The basics of these 2 hot programming approaches

@AnilAwadh

+AnilKumarMVP

http://CodePattern.net/

Page 2: Asynchronous and parallel programming

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

Page 3: Asynchronous and parallel programming

Laying the groundwork -

In Developer Eyes

Page 4: Asynchronous and parallel programming

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.

Page 5: Asynchronous and parallel programming

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.

Page 6: Asynchronous and parallel programming

Mechanism to achieve

these 2 scenario/ idea

Page 7: Asynchronous and parallel programming

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.

Page 8: Asynchronous and parallel programming

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.

Page 9: Asynchronous and parallel programming

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.

Page 10: Asynchronous and parallel programming

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.

Page 11: Asynchronous and parallel programming

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.

Page 12: Asynchronous and parallel programming

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.

Page 13: Asynchronous and parallel programming

Thank you@AnilAwadh

+AnilKumarMVP

http://CodePattern.net/