Channels in Kotlin
On this page (12sections)
Introduction
Channels is a fundamental concept every Kotlin developer should understand. Advanced coroutine topics cover cancellation and channels for safe communication between concurrent tasks.
Channels pass values between coroutines in a producer-consumer pattern. In this tutorial you will learn the syntax, walk through a complete example program, study the sample output, and review best practices so you can apply the concept confidently in your own projects.
Definition
- Channels pass values between coroutines in a producer-consumer pattern.
- A Channel can be rendezvous, buffered, or unlimited depending on capacity.
- Use channels when coroutines need to communicate streams of data safely.
Syntax
val channel = Channel<Int>()
launch { channel.send(1) }
launch { println(channel.receive()) }
Channels in Kotlin Example Program in Kotlin
import kotlinx.coroutines.*
import kotlinx.coroutines.channels.Channel
fun main() = runBlocking {
val channel = Channel<Int>()
launch {
for (x in 1..3) {
channel.send(x)
}
channel.close()
}
for (value in channel) {
println("Received: $value")
}
}
Sample Output
Received: 1
Received: 2
Received: 3
When to use
Use cancellation when a screen closes or a request is aborted; use channels when coroutines must exchange a stream of values.
How it works
-
The program starts with a
mainfunction — the entry point that runs when you execute the file. -
Channels pass values between coroutines in a producer-consumer pattern.
-
Run the program in IntelliJ IDEA, Android Studio, or with the Kotlin command-line compiler (
kotlinc/kotlin). Compare your console output with the sample output shown below.
Best Practices
- Always use structured concurrency scopes so cancellation propagates correctly.
- Close channels when production is finished to avoid leaking coroutines.
- Prefer Flow over Channel for many reactive stream use cases.
Common Mistakes
- Skipping the example and only reading the definition — hands-on practice cements the concept.
- Copying syntax without understanding nullable vs non-nullable types or scope rules.
- Ignoring compiler warnings that often point to safer alternatives.
Key Points
- Channels pass values between coroutines in a producer-consumer pattern.
- A Channel can be rendezvous, buffered, or unlimited depending on capacity.
- Use channels when coroutines need to communicate streams of data safely.
- Test the example locally and verify the output matches the sample.
- Experiment by changing input values to see how behaviour changes.
Notes
- Add the
kotlinx-coroutines-coredependency when running coroutine examples outside Android or IntelliJ. - Semicolons at the end of statements are optional in Kotlin.