56. Channels
A Channel is a coroutine-friendly queue: one coroutine sends values and another receives them, with suspension instead of blocking when the channel is empty or full. It is the streaming counterpart to a single Deferred. Note: requires the kotlinx-coroutines-core dependency.
import kotlinx.coroutines.*
import kotlinx.coroutines.channels.*
fun main() = runBlocking {
val channel = Channel<Int>()
// Producer: send a few squares, then close the channel.
launch {
for (x in 1..5) {
channel.send(x * x)
}
channel.close() // signals no more elements
}
// Consumer: iterate until the channel is closed.
for (value in channel) {
println(value)
}
println("done")
}
Each send suspends until the consumer is ready to receive, so values flow through in order. Calling close() marks the end of the stream; the for loop over the channel finishes cleanly once all sent values have been received.
Running it:
$ kotlin run
1
4
9
16
25
done
| ← Prev | Index | Next → |