@Test
fun `test select channel`() = runBlocking<Unit> {
val channels = listOf(Channel<Int>(), Channel<Int>())
GlobalScope.launch {
delay(100)
channels[0].send(200)
}
GlobalScope.launch {
delay(50)
channels[1].send(100)
}
val result = select<Int?> {
channels.forEach { channel ->
channel.onReceive { it }
}
}
println(result)
}

Kotlin select 多路复用 by Channel_开发语言

如果修改下时间

Kotlin select 多路复用 by Channel_kotlin_02

 则结果打印200

Kotlin select 多路复用 by Channel_kotlin_03