ProAndroidDev

The latest posts from Android Professionals and Google Developer Experts.

Follow publication

Member-only story

Kotlin Coroutines in Android

Satya Pavan Kantamani
ProAndroidDev
Published in
10 min readJun 27, 2021

Introduction

As we all know by default all the components in an Android application use the same thread of execution which is nothing but our Main Thread. As the application was single-threaded this main thread has many duties to perform like drawing the views, executing logical pieces of code in a sequential manner, etc. So it was our responsibility to make sure that we are not blocking the main thread. To do this we need a multi-threaded approach in some cases like performing network operations, heavy logical operations, etc. As these long or short running tasks take time we shouldn’t hold the main thread until they are completed because it freezes the app making it unresponsive.

Problem

Multithreading in Android has always been a challenge because of the callback mechanism. It has been a challenge because switching threads and resuming tasks is not so easy. It started with Async task then Rx Java and now Coroutines.

In this post lets explore how to do multithreading in a simpler way using Kotlin Coroutines. Let’s explore most of the stuff related to Coroutines. If you directly want to check the code base go through android_coroutine_sample

What is a Coroutine?

From Kotlin docs:

One can think of a coroutine as a light-weight thread. Like threads, coroutines can run in parallel, wait for each other and communicate. The biggest difference is that coroutines are very cheap, almost free: we can create thousands of them, and pay very little in terms of performance. True threads, on the other hand, are expensive to start and keep around. A thousand threads can be a serious challenge for a modern machine.

So we can say coroutines are nothing but lightweight threads. Coroutines provide us…

Create an account to read the full story.

The author made this story available to Medium members only.
If you’re new to Medium, create a new account to read this story on us.

Or, continue in mobile web

Already have an account? Sign in

Published in ProAndroidDev

The latest posts from Android Professionals and Google Developer Experts.

Written by Satya Pavan Kantamani

Android Dev, Interested in Traveling, App development. Based in Hyderabad, India. Catch me at https://about.me/satyapavankumar

Write a response