【发布时间】:2019-07-13 18:59:58
【问题描述】:
与
fun main(args: Array<String>) {
runBlocking {
withTimeout(1300L) {
repeat(1000) { i ->
println("I'm sleeping $i ...")
delay(500L)
}
}
}
}
异常崩溃:
I'm sleeping 0 ...
I'm sleeping 1 ...
I'm sleeping 2 ...
Exception in thread "main" kotlinx.coroutines.TimeoutCancellationException: Timed out waiting for 1300 ms
at kotlinx.coroutines.TimeoutKt.TimeoutCancellationException (Timeout.kt:128)
at kotlinx.coroutines.TimeoutCoroutine.run (Timeout.kt:94)
at kotlinx.coroutines.EventLoopImplBase$DelayedRunnableTask.run (EventLoop.kt:307)
at kotlinx.coroutines.EventLoopImplBase.processNextEvent (EventLoop.kt:116)
at kotlinx.coroutines.DefaultExecutor.run (DefaultExecutor.kt:68)
at java.lang.Thread.run (Thread.java:745)
但块在launch内
fun main(args: Array<String>) {
runBlocking {
launch {//<===
withTimeout(1300L) {
repeat(1000) { i ->
println("I'm sleeping $i ...")
delay(500L)
}
}
}//<===
}
}
那么超时也不例外:
I'm sleeping 0 ...
I'm sleeping 1 ...
I'm sleeping 2 ...
为什么launch没有例外?
【问题讨论】:
标签: exception kotlin kotlin-coroutines