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)
}
}