Multi-Threading vs Asynchronous programming. What is the difference?

When dealing with computers and coding, you may have probably come across the terms Multi-threading or Asynchronous programming.

Arshdeep G
Dev Genius

--

Ever wondered what they actually mean? Well, understanding the difference between the two can help any programmer to code better.

Comparasion of Synchronous and Asynchoronous.
Synchronous vs Asynchronous

Both Multi-threading and Asynchronous programming are different forms of concurrency, however, they both have their own special uses. Before we get into what they both are, we must understand what a thread is. Threads in computer science are the smallest sequence of processing, they are small series of executions that are performed within a process. These processes are all performed within the operating system and are coded by the programmer.

In a non-threaded environment, if the function has to wait for a particular event/input to happen before executing the next sequence. It MUST wait for that event to occur and finish before it can continue to perform any kind of further operations. This may be tolerable when you performing very simple tasks, however, when dealing with anything bigger than 1+1, this doesn't work well at all. And that’s where multithreading technology really comes into play.

A good way to remember this is: Threading is about the workers; Asynchrony is about the tasks.

The first Multithreading CPUs originated back in the 1950s, however, it was not until 1968 when IBM researchers were able to develop simultaneous multithreading which really changed the game for how computers worked. In a Multi-threaded environment, there exist many individual threads of programming that are all running at the same time, which helps the CPU complete complex computations at a lightning speed (also depends on the number of CPUs and OS, sometimes very complex scheduling algorithms in OS can also help create a similar Multithread effect).

In an Asynchronous environment, a single process thread runs all the time and it has the ability to switch from one function to another. In this, whenever an event happens and hits a point where it has to wait for a response/event to occur before continuing, it has the ability to start the computations for another function while it waits. For this reason, event-driven asynchronous programming is able to avoid many of the problems which a traditional multi-threaded program may have since they are difficult and can sometimes have cases where threads lock each other's memory.

In simpler terms, you can think of it like making eggs and toast:

Synchronous Approach:

  1. Get the materials required (eggs, pan, bread).
  2. Turn stove on.
  3. Crack eggs and put them in the pan.
  4. Wait for them to finish cooking.
  5. Take them off the pan.
  6. Put bread inside the toaster.
  7. Wait for the toast to finish cooking.
  8. Take toast out & Enjoy.

Total cooking time: 15 min.

Everything is done in serial order by one worker. We cannot go forward until the last step fully completes.

Asynchronous Approach:

  1. Get the materials required (eggs, pan, bread).
  2. Turn on the stove.
  3. Crack eggs and put them in the pan.
  4. Wait for eggs to finish.
  5. While waiting for eggs to finish cooking- put toast in the toaster.
  6. Wait for the toaster to finish.
  7. Once eggs complete, take the eggs off the stove.
  8. Once toast finishes, take out toast.

Total cooking time: 10 min

Here as you may notice, it requires less time to finish the meal as we are working asynchronously- so while we wait for the eggs to cook we start the task of toasting the bread.

Multi-threading Approach:

P1= person 1

P2= person 2

  1. P1: Take out eggs, bread, and pan. Turn on the stove.
  2. P1: Crack eggs and pour into pan. P2: Put bread in the toaster
  3. P1: Wait for eggs to cook. P2: Wait for bread to toast.
  4. P1: Take eggs off pan. P2: Take out toast

Total time: 5 min.

Since here we have 2 different workers (threads), each is assigned a sequence of tasks to complete. This is referred to as synchronous multithreading since neither P1 or P2 performs more than 1 task at a time. When performing complex resource-heavy tasks, Multi-threading can save a lot of time since tasks can be completed simultaneously.

So which one is really better? Multi-threading or Asynchronous?

Well, the answer to that lies within the purpose of the program and the tasks it has to perform. If it has a lot of input/output and simple computing needs then Asynchronous should fit your needs. However, if the task at hand is something that requires some computing power, then multi-threading is definitely the way to go.

--

--

“Once you have ruled out the impossible, whatever remains, however the improbable, must be true. “- Sherlock Holmes