【发布时间】:2018-06-03 03:56:57
【问题描述】:
根据文档,cancelChildren 应该取消协程的子级,但不影响父级(“此作业本身的状态不受影响。”)但是,如果我有类似
的代码 val outer = launch {
try {
launch (coroutineContext) {
try {
// work here
} catch (ex: Exception) {
println("In inner catch")
} finally {
println("In inner finally")
}
}
delay(5000) // so the outer job is running when I cancel it
} catch (ex: CancellationException) {
println("In outer catch")
} finally {
println("In outer finally")
}
}
delay(100) // give it a chance to run
outer.cancelChildren()
然后我看到以下内容
In inner catch
In inner finally
In outer catch
In outer finally
为什么“外部”工作会被取消?
这与我调用 outer.cancel 时得到的行为完全相同(但我希望如此)
【问题讨论】: