[Kotlin] Coroutine - Cancellation and Timeouts

Cancelling coroutine execution

1
2
3
4
5
6
7
8
9
10
11
12
13
fun main() = runBlocking {
val job = launch {
repeat(1000) { i ->
println("job: I'm sleeping $i ...")
delay(500L)
}
}
delay(1300L) // delay a bit
println("main: I'm tired of waiting!")
job.cancel() // cancels the job
job.join() // waits for job's completion
println("main: Now I can quit.")
}
자세히 보기