Natsuki(开元米粉实力代购)
06/16/2017, 7:27 AMverticalLayout {
button("Delay").onClick {
log("click begin")
runBlocking {
log("delay begin")
// delay(30000L)
Thread.sleep(30000L)
log("delay end")
}
log("click end")
}
}
guys,i’m a newbee in coroutine, i tried these piece of code, when change delay to sleep, an ANR was issued by system
i want to know why delay does not issue an ANR and what is main thread doing when the delay clause is executing, would anyone be kind enough to explain this for me ?uli
06/16/2017, 7:49 AMdelay
is a suspending function. The compiler will generate a continuation for you, that is then posted to the main thread.
Simplified it will produce something like
log("delay begin")
postDelayed(30000L) {
log("delay end")
}
In the real world, state machines are used for efficient implementation:
https://github.com/Kotlin/kotlin-coroutines/blob/master/kotlin-coroutines-informal.md#state-machinesNatsuki(开元米粉实力代购)
06/16/2017, 7:53 AMpostDelay
or related mechanism is used ,i.e the log(“delay end”) method with stacktrace from onclick !! not handleMessge which should be if postDelay (or related) is useduli
06/16/2017, 8:14 AMkotlinx.coroutines.experimental.android.UI
This provides the postDelayed mechanism of scheduling the continuation back to the UIThread, avoiding all locking