← Back to Kotlin Course | Chapter 12: Coroutines | Lesson 6 of 7

Channels Basics

A Channel is like a pipe that lets one coroutine send items through it while another coroutine picks them up on the other end.

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

markup
import kotlinx.coroutines.*
import kotlinx.coroutines.channels.*

fun main() = runBlocking {
    val channel = Channel<Int>()
    launch {
        channel.send(42)
    }
    println("Received: ${channel.receive()}")
}

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

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

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

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

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

markup
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")
}
Common Mistakes
  1. Forgetting to close() a channel when no more values will be sent, which can leave a receiver waiting forever with for over the channel.
  2. Sending to (or receiving from) a channel with a full buffer/no buffer without realizing that operation suspends until the other side is ready.
  3. Confusing a Channel (a communication pipe between coroutines) with a Flow (a cold, restartable stream) -- channels are hot and single-run.
Chapter Summary
  • Channel<T>() creates a communication pipe that one coroutine can send() values into while another receive()s them.
  • A for loop 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:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.