Simple explanation on Concurrency vs Multi-threading vs Asynchronous Processing
There is a common misconception on Multi-threaded, Asynchronous and concurrent processing. Some even think that they are same as their definitions overlap, and they are often used interchangeably.
First we need to understand the terms threads, synchronous, Asynchronous, concurrency and multi-threading to understand the concepts.
Without going in depth details on what a thread is lets consider a thread as a worker to do a tasks.
Synchronous Processing in simple term is doing tasks one after the other.
Synchronous Processing (single-threaded):
Lets assume we have 5 tasks to be completed. In synchronous Processing with single threading, each task will be completed one after another. Each task will be queued and the thread which is processing will execute each task one by one.
For example, lets assume there is only one ATM machine in a remote area. In order to withdraw cash, you need to use the only available machine and if there are more people they wait in queue and each person withdraws cash synchronously. This is a simple example on how processing is done for synchronous process in a single-threaded environment.
Synchronous Processing (multi-threaded):
Again we have 5 tasks to be completed. In synchronous Processing with multi threading, each task will be picked and processed by the available threads. Lets assume we have 5 threads and 10 tasks. In this case, Each thread will take a tasks which are queued. So 5 threads will pick and process 5 tasks while the remaining 5 tasks will be queued until anyone of the threads completes the task and picks another task from queue.
Lets understand this with the same ATM example, lets assume there is only 5 ATM machine in a remote area and there are 10 people waiting to withdraw cash. In order to withdraw cash, the first 5 people in queue will use all the 5 ATMs and rest of the 5 people will be queued up sequentially. Once someone completes their withdrawal, the other person waiting in the queue starts using the machine. This is a simple example on how processing is done for synchronous process in a multi-threaded environment.
Asynchronous processing in simple term is not having to wait for one task to finish before starting another.
Asynchronous Processing (single-threaded):
In real world scenarios, there wont be a Asynchronous processing in a single-threaded environment but lets understand theoritically how this works for us to understand it better. Lets say we have 5 tasks and single thread to process the tasks. In this case, the thread will work on all the tasks depending on the task priority. However, all the tasks will be be completed at the same time. What exactly happens is the processing thread takes the task 1 starts processing. During the processing phase, it suspends the execution process and saves the state of processing task 1 then it proceeds to pick and process task 2 and so on.
Example: You (thread/worker) are composing an email meanwhile you get a call so you pause email composition and attend the call. Once you finish the call, you resume the email composition . This is one simple toneddown example of how Asynchronous Processing works in a single-threaded environment.
Asynchronous Processing (multi-threaded):
It is almost same as single Asynchronous processing however there are multiple threads involved. Each thread will pick tasks and process. Depending on number of factors, thread might pause execution and pick another task before finishing the previous task.
From the picture above we can see that some tasks were handled by multiple threads asynchronously.
In simple terms concurrent processing means doing multiple taks at the same time.
As explained in Asynchronous single and multi-threaded Processing, several tasks were handled at the same time even though the tasks were in multiple states (processing, suspended). For example, listening to streaming audio and browsing at the same time.
Keep in mind that threading is about workers and asynchrony is about tasks.
I have tried to explain the differences and the interrelation between Concurrency, Multi-threading and Asynchronous Processing. Hopefully it was helpful and if you have a different example or understanding please do let me know in comments.