Talking about Kotlin Coroutines
Tags: kotlin, coroutines
Published: 2023-03-14 16:37:07

Nowadays, most web applications need to be asynchronous to attend your requests. There are many JVM libraries to work with this paradigm. The most famous is RxJava and Reactor. We can use both libraries with Kotlin, but there is an option that is better. This option is Kotlin Coroutines and we will understand how it works in this post. 

## Using Kotlin Coroutines
Kotlin Coroutines introduce the ability to suspend a coroutine at some point and resume it in the future. We might run our code on the Main thread and suspend it when we request data from API. When the coroutine is suspendend, the thread is not blocked and is free to go (Moskata, 2022, p. 15)

## Suspension Work
Suspending functions are the essential feature of Kotlin Coroutines. When we suspend functions, it means that we can stop the process in the middle, do other things, and return to finish the previous process from the point where we stopped. How coroutines does do that? When one function is suspended, it returns an object called `Continuation`, which is the responsible for marking the point where the function stopped.

##   Built in support vc library
Coroutines consist of two components: Built-in support provided by the Kotlin Language and Kotlin coroutines library. Let's see the difference:

> Built-in Support
Elements are in the kotlin.coroutines package. 
Provides a few basic elements like Continuation or suspend keyword. 

> Coroutines Library
It is another dependency
Elements are in the kotlinx.coroutines package
Provides many elements like launch, async and so on (we will see about these elements). 

## Builders
One suspend function should call another suspend function that call another suspend function and so on. But when it starts? To move from normal world to coroutines world, we need a coroutines builder. We will know about 3 builders. Let's see.


* launch builder: Similar to work with thread. We start a coroutine and it will run independently. 

```kotlin
fun main() {
     GlobalScope.launch {
          delay(1000L)
          println("World")
     }
println("Hello,")
Thread.sleep(2000L)
}
```

Result of: 
Hello,
(1 sec)
World

*Without Thread.sleep this function would end immediately after launching the coroutines.*

* runBlocking builder: You should never block threads, just suspend them, but there are some cases that is necessary. Like the last example, we needed to block the thread, because our program could end early. In this cases, we can use runBlocking. 

```kotlin
fun main() {
     runBlocking {
          delay(1000L)
          println("World")
     }
println("Hello,")
}
```

Result of: 
(1 sec)
World
Hello,

* async builder:
The async builder is similar to launch, but there is a return. 

```kotlin
fun main() {
     runBlocking {
          val result: Deferred<Int> = GlobalScope.async {
               delay(10000L)
               42   
          }
     val resultInt: Int = result.await()
     println(resultInt)
     }
}
```

Result of:
42

*async is very similar to launch, so if you replace one to another, still work fine, but don't do that, because async is about producing a value. If you don't need a value, use launch.*

## Conclusion 
As we can see, coroutines is a great option to use for asynchronous programing. My idea was to show some basic features for you start. There are many other features to cover and my intention is to write about in the next posts. 

References
Moskata, M. Kotlin Coroutines Deep Dive, 2022