Coroutine in kotlin
Coroutines are akin to threads in other languages. They facilitate asynchronous code execution separately from the main thread. When you need to run a process that might block the main thread, Kotlin allows you to create a coroutine. This creates a lightweight thread, enabling the execution of the code independently.
fun main() = runBlocking { // this: CoroutineScope
launch { // launch a new coroutine and continue
delay(1000L) // non-blocking delay for 1 second (default time unit is ms)
println("World!") // print after delay
}
println("Hello") // main coroutine continues while a previous one is delayed
}
output :
Hello
world!