【发布时间】:2019-11-19 02:31:47
【问题描述】:
我想创建两个进程守护进程,例如 aGroup 和 bGroup。
但是,当 aGroup 没有 dealy 时,bGroup 永远不会启动
谁能告诉我为什么会这样?以及使守护进程与协程永远运行的最佳方法是什么。
谢谢
@Test
fun `test`() {
runBlocking {
val one = async(start = CoroutineStart.LAZY) {
while (true) {
runAGroup();
}
}.start()
(1..10).forEach {
async(start = CoroutineStart.LAZY) {
while (true) {
runBGroup(it)
}
}.start()
}
}
}
suspend fun runAGroup() {
println("[AGroup] Main")
// delay(1000L) <--- here
}
suspend fun runBGroup(name: Int) {
println("[BGrouop] $name (1000L)")
delay(1000L)
}
【问题讨论】:
标签: multithreading kotlin kotlin-coroutines