jkbbwr
04/13/2017, 7:29 PMkotlin
suspend fun countdown() {
for (i in 1..10) {
await(delay(1000))
print("T-Minus $i")
}
}
fun main(args: Array<String>) {
run {
await(countdown())
}
}
Anything marked with suspend
can be awaited. await will simply wait until the completion
of what was marked and then return anything that was returned.
kotlin
suspend fun countdown() {
while (true) {
yield()
}
}
fun main(args: Array<String>) {
run {
spawn(countdown())
spawn(countdown()).join()
}
}
yield
explicitly gives up control. spawn
takes a suspend
and creates a Job|Task
from it.
These tasks are joinable and run concurrently with other tasks and can be canceled.