在查看示例https://github.com/kotlin/kotlinx.coroutines/blob/master/kotlinx-coroutines-core/jvm/test/guide/example-sync-07.kt时,更具体地说,在以下片段中
// This function launches a new counter actor
fun CoroutineScope.counterActor() = actor<CounterMsg> {
var counter = 0 // actor state
for (msg in channel) { // iterate over incoming messages
when (msg) {
is IncCounter -> counter++
is GetCounter -> msg.response.complete(counter)
}
}
}
我不禁注意到,例如,如果另外5个协程向上述参与者发送GetCounter消息,它们将被依次“依次”服务,即无法实现并行性。即使我们有一个具有8个物理线程的CPU,读取“计数器”值的每个“客户端”协程也会受到actor协程顺序执行的约束。
使用演员协程时如何避免这种顺序处理?
谢谢