Asynchronous Programming Masterclass in Android

Hitesh Kohli
4 min readMar 9, 2024
Photo by Fotis Fotopoulos on Unsplash

Asynchronous Programming Masterclass in Android

Asynchronous programming is among the most difficult concepts to understand in any development lifecycle. Some topics are confusing if you are someone new or even a veteran. Therefore, I tried to break down the basics of asynchronous programming in Android development.

What is asynchronous programming?

According to Google, “Asynchronous programming is a way for a computer program to handle multiple tasks simultaneously rather than executing them one after the other”. When a processor executes multiple parallel operations then it is called Asynchronous programming.

Let us suppose that the processor has multiple programs running. A processor uses threads to execute these programs. A thread can only execute one program at a time. Therefore, if we need to execute multiple programs together then we need to stop one program and switch to another program. The switching process is called Context switching. The processor switches between different programs so seamlessly as if they are executing in parallel.

But, what about multiple threads of CPU?

Nowadays, processors are not single core instead they have multiple cores with multiple threads. So, they can execute multiple tasks in parallel on different threads. Therefore, the bottleneck is not CPU threads anymore instead it has become cores. Processes run on thread but they are executed on cores. A single core can handle at most 2 threads at a time. So, context switching has become more significant to handle multiple processes on the OS.

Therefore, it has become even more important to learn about asynchronous programming and managing asynchronous calls.

We can handle asynchronous programming in Android with the following methods:

→ Kotlin Coroutines

→ Threads

→ RxJava/RxKotlin

→ Workmanager

Kotlin Coroutines:

  • Kotlin Coroutines can be understood as concurrent routines. Routines are like functions that are being executed on a thread. Concurrent means that they are being executed in parallel. Kotlin Coroutines gives us the ability to switch between threads. So that our Android UI does not get stuck and we can perform heavy tasks efficiently.
  • If you want to learn more about Kotlin coroutines →

Threads:

  • As I have explained previously, threads help us execute the processes of our app. They are a part of the CPU architecture. However, they are costly as compared to Kotlin Coroutines. As there are limited no of threads that can be executed. But, many coroutines can be created easily.
  • If you want to learn more about Threads →

RxJava/ RxKotlin:

  • RxJava used to be the standard library in the older days of Android development. But, since the introduction of Kotlin Coroutines. It has become much easier to learn and perform asynchronous tasks in Android.
  • If you want to learn more about RxJava →

WorkManager:

  • Workmanager allows us to execute non-deferrable tasks even if the app closes or restarts. Tasks that are important to execute but can be executed later on are performed by the work manager.
  • If you want to learn more about WorkManager →

I hope you enjoyed this small blog about asynchronous programming. Thank you for reading. Also If I have written something wrong please forgive me.

Also, you want to know more about me →

https://hiteshkohli.com/

--

--

Hitesh Kohli

I will help you build distribution with apps |Building and Designing apps| Writing Newsletters 📩| Building @Developcommute & Niwa