The comment in example code says delay() is non-blocking. Should it be suspending ?
https://kotlinlang.org/docs/reference/coroutines/basics.html
fun main() {
GlobalScope.launch { // launch new coroutine in background and continue
delay(1000L) // non-blocking delay for 1 second (default time unit is ms)
println("World!") // print after delay
}
println("Hello,") // main thread continues while coroutine is delayed
Thread.sleep(2000L) // block main thread for 2 seconds to keep JVM alive
}
delay is suspending and non-blocking.
TL;DR: delay does have the effect of "waiting" before executing the statement that follows it in the current coroutine. Non-blocking simply means that during this wait, the current thread can do something else.
The Kotlin documentation often says "non-blocking" for suspending functions to make it clear that they don't block the current thread, but instead simply suspend the current coroutine.
It may be misleading sometimes because "non-blocking" places the emphasis on the fact that nothing is blocked, while it should still be made clear that suspending functions do suspend the current coroutine (so at least something is kinda blocked, even if the thread itself carries on).
The fact that they suspend the current coroutine make these functions appear synchronous from the point of view of the current coroutine, because the coroutine needs to wait for these functions to complete before executing the rest of the code. However, they don't actually block the current thread because their implementation uses asynchronous mechanisms under the cover.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With