阅读背景:

Kotlin BroadcastChannel 一对多广播

来源:互联网 

相对于普通的channel


    @Test
    fun `test broadcast`() = runBlocking<Unit> {
        val broadcastChannel = BroadcastChannel<Int>(Channel.BUFFERED)
        GlobalScope.launch {
            List(3) {
                delay(100)
                broadcastChannel.send(it)
            }
            broadcastChannel.close()

        }

        List(3) { index ->
            GlobalScope.launch {
                val receiveChannel = broadcastChannel.openSubscription()
                for (i in receiveChannel) {
                    println("[#$index] received :$i")
                }
            }
        }.joinAll()
    }

    @Test
    fun `test br



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: