Skip to content

Instantly share code, notes, and snippets.

@mayojava
Created November 7, 2018 18:52
Show Gist options
  • Save mayojava/4835c95e9de4b125d0a324b8aacc87b1 to your computer and use it in GitHub Desktop.
Save mayojava/4835c95e9de4b125d0a324b8aacc87b1 to your computer and use it in GitHub Desktop.
Conflated BroadcastChannel
fun main() = runBlocking<Unit> {
val channel = ConflatedBroadcastChannel<Int>()
launch {
channel.consumeEach {
println("first: $it")
delay(500)
}
}
launch {
channel.consumeEach {
println("second: $it")
delay(300)
}
}
for (i in 1..10) {
delay(100)
channel.send(i)
}
launch {
channel.consumeEach {
println("-->later: $it")
}
}
}
//console output
first: 1
second: 1
second: 3
first: 5
second: 6
second: 9
-->later: 10
first: 10
second: 10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment