Parallel and Asynchronous Programming


Introduction

If you are  a developer and ever working with any of the Desktop or Web Application in your life, you should come up with two terms Parallel and Asynchronous programming. Although the are closely used with each other and  there are a lot of underlying similarities in implementation instead it’s worth to know the basic differences of Parallel and Asynchronous Programming and use them in the appropriate ways.

Dive Into

Let’s talk about two different scenarios as a developer when you try to implement a functionality for your application.

Scenario 1: You want to complete a Task in the shortest possible time by using your all Resources like CPU core, Memory  by dividing the task into smaller tasks.

rs_1024x759-130703153323-1024-despicableme2-1-7-3-13

All modern CPUs have multiple cores and you want to utilize the cores of CPU by distributing the job. If you can break your task into independent pieces of work, then what? Would you not like to get these independent pieces of work done in parallel to finish faster? This is called parallelism. In this case, there must be available free resources to do the job in parallel. (A resource could be cores of a CPU or even multiple machines.)

Scenario 2: You want to do a long running Task without hold your all other Tasks instead you will continue to process other Tasks in the same time. When the Task will be completed you may perform other actions based on that.

despicable_me_2_by_dave1094-d6g57fg.png

Suppose you want to add a Login feature in your application. To make it more concrete, your application is a Desktop application. So you have added a Button in your application to enable Login action. When the user clicks the Login button an Event will be fired and it will go through the Login service. But you don’t want to block your UI thread until the Login functionality performed the actions. In this case, you have to think about asynchronous programming.

Well, I am confused again!!!!!!!!

Well let’s think in a simple way:

Hey you, perform this task and return with the results. Meanwhile, I will do nothing but wait for you(Block other task execution of this context). You can smartly use other free resources/cores of the CPUs/machines to boost the work time. ( Parallel Programming)

Hey you, perform this task and return with the results. Meanwhile, I will get done other tasks. (No wasting of time. :)) (Asynchronous Programming)

Again look into the Underlined  texts. These two simple texts are enough to differentiate Parallel and Async programming.

Hold on. Wait a minute!!!!! So, it seems I can use Async with Parallel programming????

The Answer is YES!!!!!!!!!!

“An Asynchronous Task might use Parallelism”

1-qetQ9kESbuqpF3Q31aFDng.jpeg

Next Steps

This article is tried to give everyone a basic understanding Parallel and Async programming and why it’s necessary to use these techniques on building applications. Next, I will demonstrate the Parallel and Async features in .NET and how we can use them in both Desktop and Web applications, Deep Dive into the Async programming and prevent Deadlocks.

Leave a comment