Simple coroutine that delays 1 second without blocking
suggest change(from the official guide; CommonPool in that guide is now Dispatchers.Default)
import kotlinx.coroutines.*
fun main() = runBlocking {
launch(Dispatchers.Default) { // was launch(CommonPool)
delay(1000L) // non-blocking delay for 1 second (default time unit is ms)
println("World!") // print after delay
}
println("Hello,") // main function continues while the coroutine is delayed
delay(2000L) // suspend instead of blocking, long enough for World! to print
}
result
Hello,
World!
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents