DEV Community

João Victor Martins
João Victor Martins

Posted on

Talking about Kotlin Coroutines

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.
fun main() {
     GlobalScope.launch {
          delay(1000L)
          println("World")
     }
println("Hello,")
Thread.sleep(2000L)
}
Enter fullscreen mode Exit fullscreen mode

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.
fun main() {
     runBlocking {
          delay(1000L)
          println("World")
     }
println("Hello,")
}
Enter fullscreen mode Exit fullscreen mode

Result of:
(1 sec)
World
Hello,

  • async builder: The async builder is similar to launch, but there is a return.
fun main() {
     runBlocking {
          val result: Deferred<Int> = GlobalScope.async {
               delay(10000L)
               42   
          }
     val resultInt: Int = result.await()
     println(resultInt)
     }
}
Enter fullscreen mode Exit fullscreen mode

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

Top comments (6)

Collapse
 
cpdaniiel profile image
Daniel Cavalcanti

another amazing job man. thk U

Collapse
 
j_a_o_v_c_t_r profile image
João Victor Martins

Thank you, Daniel!!

Collapse
 
freeluke_ profile image
Lukas Henrique

Great article!

Collapse
 
j_a_o_v_c_t_r profile image
João Victor Martins

Thank you, man!! =)

Collapse
 
paulo_iggor profile image
Paulo Igor

Gran trabajo mi hermano!

Collapse
 
j_a_o_v_c_t_r profile image
João Victor Martins

Gracias, Paulo