Understanding Multi-Threading with Co-routines & Threads (Parallelism vs Concurrency)

Manoj Mohanty
5 min readJan 14, 2020

--

This article will help you understand concepts related to threading, parallelism, concurrency using real life analogy.

In simple terms, multitasking means dealing with more than one task at the same time e.g. watching TV while having your food, taking minutes of meetings while listening to what the other guy is saying and in terms of computing it means executing or running more than one program at the same time e.g writing some notes on a notepad and at the same time listening to song from any music streaming application

1. Process-Based Multitasking

Before we proceed with Threads & Co-routines its important to understand Process. Let’s do a simple analogy of a computer w.r.t to a room, we have an operating system and similarly, we have a caretaker. Next, let’s assume we have 4 beds in a room, 1 kitchen area, and 1 bathroom, and w.r.t our computer we have CPU with 4 cores, shared ram, and multi-level cache (cache is fast but storage is quite less) and a physical drive.

So caretaker makes sure if any person (process/program in a computer) who wants to stay in the room need to follow some rules and at the end of the day caretaker (OS in a computer) is the one who can make any decision as and when required for the proper functioning of the room. Next, let’s assume 1 person requests for stay the caretaker does some validation and assigns 1 bed in the room along with this the person gets his cupboard a table, now let’s assume 3 more people come and ask for stay caretaker does some validation and assigns beds and other stuff as required.

As in the above example, each person has their table, cupboard, bed and they don’t care or look into others cupboard neither they jump nor move to others bed, they use the bathroom as and when the time is scheduled so others get an opportunity to use the resources, the same way each process or program have access to their own assigned ram, cache and other resources necessary to execute the program correctly, In terms of computing the assignment is too fast that normal user can’t understand the difference and OS makes sure with available resources things run correctly, when a user starts loading too much program then it becomes tough for OS to handle the load and we end up seeing things like not responding, waiting for a response, etc.

2. Thread Based Multitasking (Parallelism)

Now let’s say 2 guys (A is front-end developer, B is back-end developer) who wants to start their startup and don’t have enough money they walk to our imaginary caretaker asking for stay and register themselves as one entity which means they get one bed, one table, one cupboard, and other stuff which 1 person would have got. Next, A uses the table and starts his development and requests the B to use the bed to do his work and they both use the same cupboard to store their stuff. The benefit here is 2 guys are working on two different things though towards the same goal of getting their startup running are saving money and time.

Now in terms of an application lets say you are using music streaming application where on UI thread (main thread in terms of android) user can select any listed songs and in the background, the song which is selected by the user is being downloaded or streamed, here both threads do the different task but the result is a smooth experience for user where he can start and stop a song on UI thread and at the same time background thread performs related stuff.

3. Co-routines (Concurrency)

So like in the above example we have 2 guys A is working on front-end and B is working on the back-end, Next, A asks B that he needs one API which will perform some x functionality and B agrees to it and he starts working on it and now our A is sitting idle as he is waiting for the B to finish the API and let him know which leads to wastage of time and other resources for A.

Basically he is not smart and this is what would happen if you start creating n number of threads and all threads do some IO operation such as make network calls, request data from Database, etc and all these threads have to wait for a response and are in idle state till any response is received which leads to under utilization of System resources.

Just to be more efficient B suggests A to move ahead with other development and tells him that he will let him know once the development of the required API is done. (So this is a promise that when the work is done the update will be send). A agrees to this and starts working on further development and when B is done with required API development he informs A and A resumes the task from where he left when he made a request to B and finishes that integration as well. With this approach A doesn’t have to wait and can utilize his time to perform other tasks that are not dependent on B’s response and make maximum use of his time and resources.

So in terms of computing co-routines are functions which can suspend and resume when their task is done from the same point where they left, this can be done within a thread or multiple threads (Read about Continuation in JAVA or Kotlin Co-routines)

Note: Above explanation is way more abstract compared to how things work in a computer 🙂

Conclusion

Co-routines

A developer has access to Co-routines. Co-routines are a great way to write asynchronous code that is perfectly readable and maintainable, You can create as many as co-routines you want without being worried about memory issues. It is lightweight compared to threads

Threads

A developer has access to Thread, can handle thread life cycle, Each thread has its own and stack but shares the same address space. There is less overhead and is relatively less expensive when compared to the process. Though threading is good but creating too much thread is never recommended. In Java, a single thread takes between 0.5 to 1 MB so creating too many threads may lead to OutOfMemoryError exception. Multiple threads can run concurrently (time-slicing that’s how single-core processor run multiple threads)

Process

OS takes care of process-based multitasking and is not exposed to the developer. Each process has its own address space, Process-based multitasking requires more overhead. A process will have one or more threads

I hope this article helped you understand multitasking in computers and if you think it did then please share it and do provide your feedback as it will help me to improve.

--

--

Manoj Mohanty

Android Engineer @PayPay | Ex Rapido, Medibuddy, Carnot Technologies, Deloitte