Kotlin Coroutines Part — 1: Basic

Mahesh Jadkar
Globant
Published in
5 min readSep 22, 2022

Asynchronous Programming best choice using coroutines.

Referred from https://github.com/Kotlin/kotlinx.coroutines

Coroutines have gained extreme popularity among Android Developers in recent times. There is no doubt Coroutines gives us amazing benefits over traditional asynchronous practices.

The first thing I want to talk about is What problems are coroutines trying to solve?

The main problem that coroutines solve, Is to simplify asynchronous programming by replacing callbacks; it also helps to manage the lifecycle and avoid memory leaks. So before going to discuss async programming let’s look at the synchronous programming problem. When we execute something synchronously, We wait for it to finish before moving on to another task. Due to this, we are facing issues like application blocks. Here is an example.

Suppose we are making a request call to fetch some user data and after getting data we just update the UI.
e.g.

When we execute the above code, we will notice that it triggers the error “Application Not Responding (ANR)” and App will become frozen or unresponsive.

Image referred from https://proandroiddev.com/kotlin-coroutines-in-andriod-ff0b3b399fa0

To fix the above problem we need to move our code in different threads with callbacks. Something like this.

Here we are executing this method on a different thread so with that our main thread will be free to perform other operations. See the below image we will get more idea.

https://proandroiddev.com/kotlin-coroutines-in-andriod-ff0b3b399fa0

Here we have solved the problem with callbacks but callbacks also have their limitations known as callback hells.

When there are too many nested callbacks, the code becomes difficult to read, also difficult to handle exceptions and maintain the code. Let’s see How coroutines solve our problem.
Kotlin coroutines approach to work with asynchronous.

What is coroutine?
We can say coroutine is similar to thread. It takes a block of code to execute synchronously and asynchronously.

Well Coroutines are light-weight threads, It means, Coroutines take less memory and time compared with the default Java threads. For example with a traditional way of thread if we create thousands of threads they might consume a lot of memory and time and throw an out-of-memory error.

Consume too much memory and throw an out-of-memory error.

A coroutine is an instance of suspend-able computation. Coroutines may suspend its execution in one thread and resume later at some specific time or in another thread.

Suspending Function

Suspend function is a function that could be paused and resumed some later time. The syntax of a suspending function is the same as the regular function but has an addition of the suspend keyword. Let’s see with the scenario: if we want to execute long-running operations and wait for them to complete and get back the result, Here with the normal function we might be introduced callbacks and also we are blocked until the result back. To overcome this issue we can use a suspending function, Suspend function have the ability to suspend code without blocking it and returns the result.

Note: We can call Suspend function only from a coroutine or another suspend function.
e.g:

What are coroutines builders?
Coroutines builders are used to create coroutines. Three coroutine builders are listed below.

  1. launch
  2. async
  3. runBlocking

Let’s explore each of them in detail

1. launch builder

So basically it launches a new coroutine without blocking a current thread. It returns a reference to the job object. Using a job object we can cancel the coroutine or wait for the coroutine to finish.
e.g:

2. Async Builder

It’s the same as a launch builder. The only difference is it returns a deferred object.
Using the Deferred object we can cancel the coroutines, wait for coroutines to finish, or retrieve the returned result.
e.g:

Note: This launch and sync never block the thread in which operates

3. Runblocking Builder:

It launches a new coroutine but it’s blocking a current thread and waits for the coroutine to finish execution.So, why do we need runBlocking when we are clearly focusing to avoid blocking the main thread?

Usually, runBlocking it is used in unit tests in Android or in some other cases of synchronous code.

e.g:

Recap :
I hope we understand the basics of coroutines now. What’s the problem we had before about coroutines? Doing async programming with coroutines is the best choice. Managing callbacks, handling exceptions and avoiding memory leaks becomes very easy with coroutines.

As we learn basic of coroutines in first part, now we are going to learn scopes and dispatchers Kotlin Coroutines Part 2: Scope & Dispatchers

Credit Tags
here are few reference links where i refer.
https://kotlinlang.org/docs/coroutines-overview.html
https://developer.android.com/kotlin/coroutines
https://proandroiddev.com/kotlin-coroutines-in-andriod-ff0b3b399fa0

--

--