Tristan Caron
11/04/2017, 2:09 PMhttp://reactivex.io/documentation/operators/images/S.PublishSubject.png▾
BroadcastChannel
?hydrakecat
11/07/2017, 2:34 PMfun main(args: Array<String>) = runBlocking<Unit> {
val broadcast = ArrayBroadcastChannel<Int>(10)
launch(CommonPool) {
broadcast.consumeEach { i -> println("channel-0:$i") }
}
broadcast.offer(1)
broadcast.offer(2)
launch(CommonPool) {
broadcast.consumeEach { i -> println("channel-1:$i") }
}
broadcast.offer(3)
broadcast.offer(4)
broadcast.close()
}
Tristan Caron
11/07/2017, 2:45 PM./AppKt
channel-0:2
channel-0:3
channel-0:4
Process finished with exit code 0
I tried this but not doing what I want either.
fun main(args: Array<String>) = runBlocking<Unit> {
val broadcast = BroadcastChannel<Int>(10)
launch {
broadcast.openSubscription().use {
for (value in it) {
println(value)
}
}
}
broadcast.offer(1)
broadcast.offer(2)
launch {
broadcast.openSubscription().use {
for (value in it) {
println(value)
}
}
}
broadcast.offer(3)
broadcast.offer(4)
}
hydrakecat
11/07/2017, 2:51 PMfun main(args: Array<String>) = runBlocking<Unit> {
val broadcast = ArrayBroadcastChannel<Int>(10)
val job1 = launch(CommonPool) {
broadcast.consumeEach { i -> println("channel-0:$i") }
}
broadcast.offer(1)
broadcast.offer(2)
val job2 = launch(CommonPool) {
broadcast.consumeEach { i -> println("channel-1:$i") }
}
broadcast.offer(3)
broadcast.offer(4)
broadcast.close()
job1.join()
job2.join()
}
boradcast.consumeEach
is called before broadcast.offer(1)
so 1
might not be printed.Tristan Caron
11/08/2017, 8:46 AMInt.MAX_VALUE
is not a valid answer I think 😛hydrakecat
11/08/2017, 10:19 AM