What are Coroutines in Kotlin?

Coroutines in Kotlin

Coroutines are great features available in Kotlin Language. I played with it and really liked it.

The mission of this post is to help you understand the Coroutines. Let’s make this mission successful by reading it carefully.

Let’s start with the official definition of Coroutines.

Coroutines are a new way of writing asynchronous, non-blocking code (and much more)

But the first question arises in our mind is that how the Coroutines are different from the threads?

Coroutine are light-weight threads. A light weight thread means it doesn’t map on native thread, so it doesn’t require context switching on processor, so they are faster.

What does it mean when I say “it doesn’t map on native thread”?

Coroutines are available in many languages.

Basically, there are two types of Coroutines:

  • Stackless
  • Stackful

Kotlin implements stackless coroutines — it’s mean that the coroutines don’t have own stack, so they don’t map on native thread.

Coroutines and the threads both are multitasking. But the difference is that threads are managed by the OS and coroutines by the users.

Now, you can understand the below paragraph, what the official website of Kotlin says

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.

Let’s see how we can use the Coroutines.

So, how do we start a coroutine(like we start a thread)?

These are the functions to start the coroutine:

  • launch{}
  • async{}

Launch vs Async in Kotlin Coroutines

The difference is that the launch{} does not return anything and theasync{} returns an instance of Deferred<T>, which has an await() function that returns the result of the coroutine like we have future in Java. and we do future.get() in Java to the get the result.

Let’ see the use of launch?

fun main(args: Array<String>) {
 println("Kotlin Start")
 launch(CommonPool) {
    delay(2000)
    println("Kotlin Inside")
 }
 println("Kotlin End")
}
// The output will be
// Kotlin Start
// Kotlin End
// Kotlin Inside

This starts a new coroutine on a given thread pool. In this case, we are using CommonPool that uses ForkJoinPool.commonPool(). Threads still exist in a program based on coroutines, but one thread can run many coroutines, so there’s no need for too many threads.

Let’s try one thing

fun main(args: Array<String>) {
 delay(2000)
}

If you directly do this in the main function, it says this with error:

Suspend functions are only allowed to be called from a coroutine or another suspend function.

Here, the delay function is suspend function, so we can only call it from a coroutine or an another suspend function.

Let’s correct it.

fun main(args: Array<String>) {
 runBlocking {
    delay(2000)
 }
}

Let’s see one more example

suspend fun doWorkFor1Seconds(): String {
  delay(1000)
  return “doWorkFor1Seconds”
}
suspend fun doWorkFor2Seconds(): String {
  delay(2000)
  return “doWorkFor2Seconds”
}
// Serial execution 
private fun doWorksInSeries() {
  launch(CommonPool) {
     val one = doWorkFor1Seconds()
     val two = doWorkFor2Seconds()
     println("Kotlin One : " + one)
     println("Kotlin Two : " + two)
  }
}
// The output is
// Kotlin One : doWorkFor1Seconds
// Kotlin Two : doWorkFor2Seconds

Now, Let’ see the use of async?

// Parallel execution
private fun doWorksInParallel() {
  val one = async(CommonPool) {
      doWorkFor1Seconds()
  }
  val two = async(CommonPool) {
      doWorkFor2Seconds()
  }
  launch(CommonPool) {
      val combined = one.await() + "_" + two.await()
      println("Kotlin Combined : " + combined)
  }
}
// The output is
// Kotlin Combined : doWorkFor1Seconds_doWorkFor2Seconds

Async is used here, so that we can use await() to get the result.

Happy Learning 🙂


Also, Let’s become friends on Twitter, Linkedin, Github, and Facebook.


Amit Shekhar

Amit Shekhar

Co-Founder at Mindorks | Learning is a journey, let’s learn together