Channels Basics
In this page:
Creating and Using a Channel
A Channel<T> acts as a pipe between coroutines: one side calls send() to push a value in, while another calls receive() to pull it out.
Warning: This example uses kotlinx.coroutines, which is not part of the plain Kotlin standard library. It requires the kotlinx-coroutines-core dependency (added via Gradle/Maven) to compile and run.
Example: Creating and Using a Channel
import kotlinx.coroutines.*
import kotlinx.coroutines.channels.*
fun main() = runBlocking {
val channel = Channel<Int>()
launch {
channel.send(42)
}
println("Received: ${channel.receive()}")
}
Login to try C/C++/Java/PHP code in the editor
Iterating Over a Channel
A for loop can consume every value sent to a channel until the channel is closed, at which point the loop ends naturally.
Warning: This example uses kotlinx.coroutines, which is not part of the plain Kotlin standard library. It requires the kotlinx-coroutines-core dependency (added via Gradle/Maven) to compile and run.
Example: Iterating Over a Channel
import kotlinx.coroutines.*
import kotlinx.coroutines.channels.*
fun main() = runBlocking {
val channel = Channel<Int>()
launch {
for (i in 1..3) {
channel.send(i)
}
channel.close()
}
for (value in channel) {
println("Got: $value")
}
}
Login to try C/C++/Java/PHP code in the editor
Closing a Channel
Calling close() on a channel signals that no more values will ever be sent, which lets any code iterating over it with a for loop finish cleanly instead of waiting indefinitely.
Warning: This example uses kotlinx.coroutines, which is not part of the plain Kotlin standard library. It requires the kotlinx-coroutines-core dependency (added via Gradle/Maven) to compile and run.
Example: Closing a Channel
import kotlinx.coroutines.*
import kotlinx.coroutines.channels.*
fun main() = runBlocking {
val channel = Channel<String>()
launch {
channel.send("done")
channel.close()
}
for (msg in channel) {
println("Message: $msg")
}
println("Channel fully drained")
}
Login to try C/C++/Java/PHP code in the editor
Channels vs Flow
A channel is a hot pipe -- each value sent is consumed exactly once by a receiver -- while a Flow is cold and can be collected fresh, from the start, as many times as needed.
Warning: This example uses kotlinx.coroutines, which is not part of the plain Kotlin standard library. It requires the kotlinx-coroutines-core dependency (added via Gradle/Maven) to compile and run.
Example: Channels vs Flow
import kotlinx.coroutines.*
import kotlinx.coroutines.channels.*
fun main() = runBlocking {
val channel = Channel<Int>(3)
channel.send(1)
channel.send(2)
channel.close()
var sum = 0
for (v in channel) sum += v
println("Sum from channel: $sum")
}
Login to try C/C++/Java/PHP code in the editor
- Forgetting to
close()a channel when no more values will be sent, which can leave a receiver waiting forever withforover the channel. - Sending to (or receiving from) a channel with a full buffer/no buffer without realizing that operation suspends until the other side is ready.
- Confusing a
Channel(a communication pipe between coroutines) with aFlow(a cold, restartable stream) -- channels are hot and single-run.
Channel<T>()creates a communication pipe that one coroutine cansend()values into while anotherreceive()s them.- A
forloop can iterate directly over a channel's values until it is closed. close()signals that no more values will be sent, allowing receivers to finish iterating cleanly.- Channels are hot -- values sent are gone once received, unlike a Flow which can be collected repeatedly.
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: