Monday 22 August 2022

Advanced Kotlin Coroutines : Introduction

 Hi, 

Today I am unwraping the topic in Kotin world i.e. Coroutine.

If you want to get started with Kotlin coroutine and ease your daily development tasks by using it , then this series is for you. 

In this post , I am starting with introduction , then in future posts we will read more interesting coroutine topics together :) 

So let's get started.

First question which comes in our mind is , 

What is the need of Coroutine?

All the components in an android application , use the same thread of execution i.e. our Main thread.

This main thread is responsible to perform many tasks like drawing the views, executing logical pieces of code in a sequential manner, etc.

So it's developers duty to make sure that main thread must not be blocked. For this reason multi threading comes in picture, with this approach the long or short running tasks which take time should run on a different worker thread to prevent the blocking of main thread and unresponsiveness of the app.

Multithreading in Android has always been a challenge due to it's callback mechanism ,switching between threads and resuming tasks. 

In android multithreading started with Async Task , then RxJava and now we have coroutine to achieve this.

What is 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.

A coroutine is a function that can pause its execution to be resumed later. You can think of coroutines as lightweight threads.

Coroutine word is made of two words : co + routine 

Co means cooperate and routine means functions . So we can say coroutine means when functions cooperate, which states : 

  • Coroutines are nothing but lightweight threads. 
  • Coroutines provide us an easy way to do synchronous and asynchronous programming. 
  • Coroutines allow execution to be suspended and resumed later at some point in the future which is best suited for performing non-blocking operations in the case of multithreading.

There are few properties of coroutines

  • They are light-weight
  • Built-in cancellation support
  • Lower chances for memory leaks
  • Jetpack libraries provide coroutines support
It was added to Kotlin in version 1.1. 

Now we understood a little bit about coroutines. 

Also there are some important concepts related to coroutines which will be covered in next posts like :
suspend, Job, Dispatchers, CoroutineScope, CoroutineContext, etc. 


Advanced Kotlin Coroutines : Introduction

 Hi,  Today I am unwraping the topic in Kotin world i.e. Coroutine . If you want to get started with Kotlin coroutine and ease your daily de...