Concurrency vs Multi threading vs Asynchronous Processing Explained

Concurrency vs Multi-threading vs Asynchronous Processing

Sim­ple expla­na­tion on Con­cur­ren­cy vs Mul­ti-thread­ing vs Asyn­chro­nous Processing

There is a com­mon mis­con­cep­tion on Mul­ti-thread­ed, Asyn­chro­nous and con­cur­rent pro­cess­ing. Some even think that they are same as their def­i­n­i­tions over­lap, and they are often used interchangeably. 

First we need to under­stand the terms threads, syn­chro­nous, Asyn­chro­nous, con­cur­ren­cy and mul­ti-thread­ing to under­stand the concepts.

Threads: #

With­out going in depth details on what a thread is lets con­sid­er a thread as a work­er to do a tasks.

Syn­chro­nous Pro­cess­ing: #

Syn­chro­nous Pro­cess­ing in sim­ple term is doing tasks one after the other.

Syn­chro­nous Pro­cess­ing (sin­gle-thread­ed):

Lets assume we have 5 tasks to be com­plet­ed. In syn­chro­nous Pro­cess­ing with sin­gle thread­ing, each task will be com­plet­ed one after anoth­er. Each task will be queued and the thread which is pro­cess­ing will exe­cute each task one by one.

For exam­ple, lets assume there is only one ATM machine in a remote area. In order to with­draw cash, you need to use the only avail­able machine and if there are more peo­ple they wait in queue and each per­son with­draws cash syn­chro­nous­ly. This is a sim­ple exam­ple on how pro­cess­ing is done for syn­chro­nous process in a sin­gle-thread­ed environment.

single-threaded Synchronous Processing

Syn­chro­nous Pro­cess­ing (mul­ti-thread­ed):

Again we have 5 tasks to be com­plet­ed. In syn­chro­nous Pro­cess­ing with mul­ti thread­ing, each task will be picked and processed by the avail­able 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 remain­ing 5 tasks will be queued until any­one of the threads com­pletes the task and picks anoth­er task from queue. 

Lets under­stand this with the same ATM exam­ple, lets assume there is only 5 ATM machine in a remote area and there are 10 peo­ple wait­ing to with­draw cash. In order to with­draw cash, the first 5 peo­ple in queue will use all the 5 ATMs and rest of the 5 peo­ple will be queued up sequen­tial­ly. Once some­one com­pletes their with­draw­al, the oth­er per­son wait­ing in the queue starts using the machine. This is a sim­ple exam­ple on how pro­cess­ing is done for syn­chro­nous process in a mul­ti-thread­ed environment.

Synchronous Processing (multi-threaded)

Asyn­chro­nous pro­cess­ing: #

Asyn­chro­nous pro­cess­ing in sim­ple term is not hav­ing to wait for one task to fin­ish before start­ing another.

Asyn­chro­nous Pro­cess­ing (sin­gle-thread­ed):

In real world sce­nar­ios, there wont be a Asyn­chro­nous pro­cess­ing in a sin­gle-thread­ed envi­ron­ment but lets under­stand the­o­rit­i­cal­ly how this works for us to under­stand it bet­ter. Lets say we have 5 tasks and sin­gle thread to process the tasks. In this case, the thread will work on all the tasks depend­ing on the task pri­or­i­ty. How­ev­er, all the tasks will be be com­plet­ed at the same time. What exact­ly hap­pens is the pro­cess­ing thread takes the task 1 starts pro­cess­ing. Dur­ing the pro­cess­ing phase, it sus­pends the exe­cu­tion process and saves the state of pro­cess­ing task 1 then it pro­ceeds to pick and process task 2 and so on. 

Exam­ple: You (thread/​worker) are com­pos­ing an email mean­while you get a call so you pause email com­po­si­tion and attend the call. Once you fin­ish the call, you resume the email com­po­si­tion . This is one sim­ple toned­down exam­ple of how Asyn­chro­nous Pro­cess­ing works in a sin­gle-thread­ed environment.

Asynchronous Processing (single-threaded)

Asyn­chro­nous Pro­cess­ing (mul­ti-thread­ed):

It is almost same as sin­gle Asyn­chro­nous pro­cess­ing how­ev­er there are mul­ti­ple threads involved. Each thread will pick tasks and process. Depend­ing on num­ber of fac­tors, thread might pause exe­cu­tion and pick anoth­er task before fin­ish­ing the pre­vi­ous task. 

From the pic­ture above we can see that some tasks were han­dled by mul­ti­ple threads asynchronously.

  • Task 1: Han­dled in Thread 1, thread 2 and thread 3.
  • Task 5: Han­dled by thread 1 and thread 2.
  • Task 6: Han­dled by thread 3 and thread 1.

Asynchronous Processing (multi-threaded)

Con­cur­rent Pro­cess­ing: #

In sim­ple terms con­cur­rent pro­cess­ing means doing mul­ti­ple taks at the same time.

As explained in Asyn­chro­nous sin­gle and mul­ti-thread­ed Pro­cess­ing, sev­er­al tasks were han­dled at the same time even though the tasks were in mul­ti­ple states (pro­cess­ing, sus­pend­ed). For exam­ple, lis­ten­ing to stream­ing audio and brows­ing at the same time. 

Con­clu­sion #

Keep in mind that thread­ing is about work­ers and asyn­chrony is about tasks. 

I have tried to explain the dif­fer­ences and the inter­re­la­tion between Con­cur­ren­cy, Mul­ti-thread­ing and Asyn­chro­nous Pro­cess­ing. Hope­ful­ly it was help­ful and if you have a dif­fer­ent exam­ple or under­stand­ing please do let me know in comments.