Asynchronous Programming

Prachi
The Startup
Published in
4 min readOct 14, 2020

--

Photo by Chewy on Unsplash

Asynchronous programming is not a new concept. It has been with us from the very early days of computing because we need to make the best usage of the hardware. But recently it has become a standard programming paradigm.

Let’s understand a few concepts before understanding asynchronous programming.

Threads

Threads are a sequence of execution of code that can be executed independently of one another. It is the smallest unit of tasks that can be executed by an OS. A program can be single-threaded or multi-threaded.

Process

An active program i.e a program under execution is called a process. A program can have multiple processes. A process usually starts with a single thread and later it creates multiple threads.

Single-Threaded and Multi-Threaded

A thread is a simple flow of instruction. An application can be single-threaded ( imagine it as a single line going from the entry point of the application to its end) or multi-threaded (imagine a tree: the whole app starts from 1 point then it branches out more and more).

So basically what I mean is — In Single-threaded, only one task is executed at a time. In Multi-threaded architecture, multiple tasks are executed at a time.

Single-Threaded-

- Execute task 1
- Execute task 2
- Execute task 3
- Execute task 4
- Execute task 5
- Execute task 6
- Execute task 7
- Execute task 8

Multi-Threaded

Thread1:

- Execute task 1
- Execute task 2
- Execute task 3

Thread2:

- Execute task 4
- Execute task 5
- Execute task 6

Thread3:

- Execute task 7
- Execute task 8

Now let’s understand the concept of Synchronous.

Synchronous Programming

To understand how synchronous programming works, let’s take an example. Imagine you are at a restaurant, there is just one waiter and he is allocated for you.

  1. He will now take your order, deliver it to the kitchen, and wait there for the chef to prepare your food.
  2. During this time, the waiter is not doing anything even if a new customer arrives.
  3. The new customers have to wait until the waiter is done serving you.

This is how synchronous programming works in single-threaded architecture. A thread is allocated to handle the task that you have requested to run. If the response to your request involves something time-consuming like a database query, the thread remains idle until the operation is complete. Meanwhile, if you have requested other tasks, then these have to wait!

Now imagine you are at a restaurant where there are 6 waiters. One waiter is allocated to you.

  1. He will now take your order, deliver it to the kitchen, and wait there for the chef to prepare your food.
  2. He will not serve anyone until he is done serving you.
  3. Meaning while 12 more customers come. The rest 5 waiters are allocated to the 5 customers (according to who comes first).
  4. The rest 7 customers have to wait until any of the waiters are done with serving.

This is how synchronous programming works in multi-threaded architecture. A new thread is allocated to a new request or task. If there is a large number of concurrent tasks, the program may run out of threads, putting new tasks to wait until a thread is available.

Using this idea we are not able to make the best advantage of the hardware. Imagine a user is interacting with your UI. There are 2 buttons- one is loading image from the server (suppose taking 3 min) and the other button is changing the font color.

If you are working on single-threaded architecture, the user clicks on button 1, he has wait for 3 min to click on button 2. NOTA GREAT IDEA!

Even if you are working on multi-threaded architecture, suppose there are no threads left, then to the user has to wait, resulting in a bad, very bad user experience!

Asynchronous Programming to the rescue

To understand how asynchronous programming works, come back to our example.

Imagine you are at a restaurant, there is just one waiter. But this time he is not particularly allocated for you.

  1. He will now take your order, deliver it to the kitchen.
  2. Meanwhile, if other customers come he will not sit idle and wait for finishing serving you.
  3. Rather he will take orders from the other customer and deliver it to the kitchen.
  4. He will serve the food to the customers according to the minimum cooking time of the dishes.

This is how the asynchronous programming works in single-threaded processes. A single thread will handle several tasks without waiting for the result. When the result is ready, the thread will collect the result and present it to you.

Asynchronous programming in multi-threaded — Multiple threads will handle several tasks without waiting for the result. When the results are ready, the threads will collect the result and present it to you.

So we can say that the asynchronous method results in better utilization of the hardware resources.

We can summarize our whole story as-

  1. In the synchronous programming model, tasks are executed one after another. Each task waits for any previous task to complete and then gets executed.
  2. In an asynchronous programming model, when one task gets executed, you could switch to a different task without waiting for the previous to get completed.

Thank you for reading!🙏🌺

--

--