![]() |
VOOZH | about |
Asynchronous programming is very important and it's now a common part of modern application. It increases the amount of work that an app can execute by allowing tasks to execute in parallel. This makes sure that heavy tasks run in the background, keeping the UI thread responsive and improving the overall user experience.
The Kotlin team defines coroutines as “lightweight threads”. They are sort of tasks that the actual threads can execute. Coroutines were added to Kotlin in version 1.3 and are based on established concepts from other languages. Kotlin coroutines introduce a new style of concurrency that can be used on Android to simplify async code.
The official documentation says that coroutines are lightweight threads. By lightweight, it means that creating coroutines doesn't allocate new threads. Instead, they use predefined thread pools and smart scheduling for the purpose of which task to execute next and which tasks later.
There are two types of Coroutines:
Kotlin implements stackless coroutines, meaning they operate without their own stack and are not directly tied to native threads.
As we know android developers today have many async tools in hand. These include RxJava, AsyncTasks, Jobs, Threads. But, these methods come with several challenges which includes:
Android is a single thread platform, By default, everything runs on the main thread. In Android, almost every application needs to perform some non UI operations like (Network call, I/O operations), so when coroutines concept is not introduced, what is done is that programmer dedicate this task to different threads, each thread executes the task given to it, when the task is completed, they return the result to UI thread to update the changes required. Though In android there is a detailed procedure given, about how to perform this task in an effective way using best practices using threads, this procedure includes lots of callbacks for passing the result among threads, which ultimately introduce tons of code in our application and the waiting time to get the result back to increases.
On Android, Every app has a main thread (which handles all the UI operations like drawing views and other user interactions. If there is too much work happening on this main thread, like network calls (eg fetching a web page), the apps will appear to hang or slow down leading to poor user experience.
Coroutines is the recommended solution for asynchronous programming on Android. Some highlighted features of coroutines are given below.
Features | Threads | Coroutines |
|---|---|---|
Resource Usage | Heavyweight (high memory/CPU usage) | Lightweight(thousands can run on a single thread) |
Context Switching | Slower, as it depends on thread completion | Faster, as coroutines can suspend and resume |
Parallelism | Native OS-supported parallelism (runs on multiple cores) | Single threaded by default by can be Multi-threaded via dispatchers. |
Creation Cost | High (Each thread has its own stack) | Low (coroutines do not require separate stacks) |
Readability | Complex (Involves multiple callbacks) | Simplified (More concise and readable code) |
Scaling | Limited by OS/hardware (hundreds of threads risk instability) | Scales effortlessly to thousands of concurrent operations |
Add these dependencies in build.gradle.kts Module-level file.
Note: Replace
x.x.xwith the latest coroutine version. As of Feb 2025, the latest and stable version of Kotlin Coroutines is 1.10.1
Let's say we want to fetch some users from the database and show it on the screen. For fetching the data from the database we have to perform the network call, fetch the user from the database, and show it on the screen. Fetching the user can be done by using either by using callbacks or coroutines.
Less readable code
This has a more cleaner code
As discussed above using callbacks will decrease the code readability, so using coroutines is much better to use in terms of readability and performance as well. As discussed above Coroutines has many advantages apart from not having callbacks, like they are nonblocking, easy, and nonexpensive to create.