Photo by Lyndon Li on Unsplash

Kotlin Coroutines

Posted: 12 Jul 2017. Last modified on 04-Jun-22.

This article will take about 2 minutes to read.


Coroutines are experimental in kotlin 1.1+

This means that they are currently stable, but the syntax for them might change.

They are enabled via the library kotlinx.coroutines, so they are not coupled together with the language

But what are they?

Threads

Coroutines:

Why should we use coroutines?

// kotlin 1.1
val fibinacciSeq = buildSequence {
    var a = 0
    var b = 0
    yield(1)
    while(true) {
        yield(a + b)
        a = b.also { b = a + b }
    }
}

Currently there are only 2 coroutine builders in kotlin-stdlib

Other coroutine builders include

we can always define custom coroutine builders via functional kotlin language constructs.

References: