Channels in Kotlin Coroutines
Channels
Channels in Kotlin Coroutines provide a way to transfer a stream of values between coroutines. They are similar to BlockingQueue, but with some key differences. For example, instead of putting an element to a queue, a sender sends it to a channel. Instead of taking an element from a queue, a receiver receives it from a channel. Here’s a simple example:
import kotlinx.coroutines.*
import kotlinx.coroutines.channels.*
fun main() = runBlocking {
val channel = Channel<Int>()
launch {
for (x in 1..5) channel.send(x * x)
channel.close()
}
repeat(5) { println(channel.receive()) }
println("Done!")
}
In this code, we’re creating a channel of Integers. Inside a coroutine, we’re sending squares of numbers from 1 to 5 to the channel, and then closing it. Outside the coroutine, we’re receiving the values from the channel and printing them.