Reading all the elements in the producer

To go through all the elements in the producer, you can use the  consumeEach() function, as follows for example:

val context = newSingleThreadContext("myThread")

val producer = produce(context) {
for (i in 0..9) {
send(i)
}
}

This producer will produce up to 10 numbers; to retrieve all of them we can simply call consumeEach() on the producer:

fun main(args: Array<String>) {
producer.consumeEach {
println(it)
}
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.149.255.168