CodeGym /Courses /Kotlin SELF /Channel as a message queue: send/ receive, capacity, and...

Channel as a message queue: send/ receive, capacity, and close

Kotlin SELF
Level 54, Lesson 0
Available

1. What is Channel, and why do you need it?

When we’re just starting to write coroutine code, the hand reaches to do something like: “I have a producer coroutine; it adds elements to a MutableList, and a consumer coroutine reads them from there.” On a tiny example this sometimes even “works,” but only until you run into data races, weird omissions, and unpredictable states.

Channel is needed precisely so that instead of shared ownership of mutable structures you move to a proper protocol: one side sends a message, the other receives it.

Let’s imagine a training console project (let it be the Console Monitor utility): one coroutine “collects events” (for example, log lines or “pseudo-metrics”), and another coroutine processes these events and prints aggregated information. We need a safe “pipeline” between them: not a shared list that everyone pokes by hand, but a normal message queue.

This looks like this schematically:

flowchart LR
    P[Producer coroutine
generates events] --> C[(Channel⟨Event⟩)] C --> R[Receiver coroutine
processes events]

Channel<T> as a typed message queue

In Kotlin coroutines, Channel is an abstraction of a “message queue” between coroutines. The key word here is messages. We don’t “share memory”; we pass values.

A channel is typed: Channel<Int> passes Int, Channel<String> passes strings, Channel<Event> passes your own events.

There’s an important detail that often breaks beginners’ expectations: a channel is not a “broadcast to all subscribers.” One sent value will be delivered to exactly one receiver (if there are multiple receivers, they “compete” for messages). That’s part of the Channel model.

So think of Channel as a “task/message queue,” not as a “group chat” where everyone must see the same message.

A mini table to lock it in:

What we want What fits better Intuition
Pass a value “from coroutine A to coroutine B”
Channel<T>
Like a queue: put it in → take it out
Store a dataset “inside the program” collections (
MutableList
,
MutableMap
)
Like a warehouse: it sits and waits
Deliver one event “to all listeners” not
Channel
(that’s a different pattern)
Like broadcasts/notifications

We’re intentionally not going deeper into “broadcast to everyone” today—our goal is to learn the basic model: a send/receive queue.

Minimal example: send and receive

The smallest working snippet to make it click in your head: a channel is a thing you can actually use to “throw a value” between coroutines.

import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.launch
import kotlinx.coroutines.channels.Channel

fun main() = runBlocking {
    val ch = Channel<Int>()

    launch { ch.send(42) }

    val x = ch.receive()
    println("Got $x") // Got 42

    ch.close()
}

What matters here is not that we printed 42, but that we saw a new way of thinking: instead of a “shared variable,” we now have a communication channel.

And one quick observation right away: send(...) and receive() work only in a coroutine context. That’s not the library authors being picky; it’s the logic of the mechanism itself: these operations can “wait,” which means they must be suspend.

Why send() and receive() are suspend

If you previously perceived suspend as “there will be a delay inside,” here’s a broader view: suspend is about operations that may suspend a coroutine without blocking a thread.

Both ends of a channel can potentially “wait”: the sender may wait until there is space, and the receiver may wait until a value appears.

Let’s break this down into an intuitive mechanism. You can think of a channel as a mailbox between two people. If the mailbox is empty, the receiver checks it—and waits until someone puts a letter in. If the mailbox is full (or has no buffer at all), the sender wants to put a letter in—and waits until someone takes something out.

Table: “who waits when”:

Operation Why it may suspend A short human phrase
ch.send(value)
“Nowhere to put it” (no space in the buffer, or buffer size is 0) “I’ll hold the message until space frees up”
ch.receive()
“Nothing to take” (the channel is empty) “I’ll wait until someone sends a message”

At this point a sensible thought often appears: “So the channel slows down the sender if the receiver can’t keep up?” Yes—and that’s a very useful property. In data-flow worlds this is often called “natural backpressure,” so the producer doesn’t flood memory with a million messages.

Capacity: the channel buffer and send behavior

Now let’s add a size to the “mailbox.” In Channel this is called capacity.

If capacity is 0, the sender and receiver meet “face to face”: sending completes only when there is a receiver. This is like handing a package from one set of hands to another.

If capacity is greater than zero, the channel starts working like a real queue: you can put several elements “ahead” without waiting for them to be taken right now.

An example with capacity = 1, where the second send can “hang” if the receiver hasn’t taken the first one yet:

import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.launch
import kotlinx.coroutines.delay
import kotlinx.coroutines.channels.Channel

fun main() = runBlocking {
    val ch = Channel<String>(capacity = 1)

    val sender = launch {
        ch.send("A")
        ch.send("B") // may wait until "A" is taken
        ch.close()
    }

    delay(50)
    println(ch.receive()) // A
    println(ch.receive()) // B

    sender.join()
}

In this example the delay(50) makes the situation visible: the producer tries to send "A" and "B" quickly, but the channel allows only one element to be pushed ahead. The second send will wait until the consumer frees up space.

A useful analogy: capacity is the size of the “box by the door.” If the box fits 1 package, the courier can leave one and go. But if they come with two, they’ll have to hold the second in their hands until you pick up the first.

close(): how to say “there will be no more messages”

Any message queue runs into a simple question: “when do we stop?” If the consumer does receive() in a loop but the producer finished long ago, the consumer may wait forever. That’s why the channel has an important part of the protocol: close().

close() means: “there will be no new elements.” At the same time, elements that are already in the channel (in the buffer) can still be read. Closing is not “erase and forget”; it is specifically a signal that the message stream is complete.

The most convenient and readable way to read a channel until it is closed is the for (x in ch) loop, which will finish correctly on its own when the channel is closed and the elements run out.

import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.launch
import kotlinx.coroutines.channels.Channel

fun main() = runBlocking {
    val ch = Channel<Int>()

    launch {
        repeat(3) { i -> ch.send(i) }
        ch.close()
    }

    for (x in ch) {
        println("x=$x") // x=0, x=1, x=2
    }
    println("Done") // Done
}

This for works precisely because a channel is a stream of values “over time,” and its completion is expressed by closing.

2. Practice: Console Monitor and the shutdown protocol

Event channel

Let’s assemble the pieces into a small “story,” so it looks less like a lab 42 and more like real code.

Let our Console Monitor do two things: generate events (as if they were incoming log lines) and print them as if it were a handler. We’re not building a “worker factory” yet and we’re not making complex pipelines—we only need the basic combination: Channel + send/receive + close.

First, let’s agree on the event type. To keep the lecture simple, we’ll use just a string:

import kotlinx.coroutines.channels.Channel

typealias LogEvent = String

fun createLogChannel(): Channel<LogEvent> = Channel(capacity = 2)

Producer: generating events and close

Now let’s make an “event generator”: it sends a few strings in a loop and closes the channel.

import kotlinx.coroutines.delay
import kotlinx.coroutines.channels.Channel

suspend fun produceLogs(ch: Channel<String>) {
    repeat(5) { i ->
        ch.send("event#$i")
        delay(30)
    }
    ch.close()
}

Consumer: reading until closed

And now the “event handler”: it reads everything until the channel is closed.

import kotlinx.coroutines.channels.Channel

suspend fun consumeLogs(ch: Channel<String>) {
    for (e in ch) {
        println("handle: $e") // handle: event#0 ...
    }
}

Wiring it up in main

All that’s left is to connect it in main:

import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.launch
import kotlinx.coroutines.channels.Channel

fun main() = runBlocking {
    val ch = Channel<String>(capacity = 2)

    val producer = launch { produceLogs(ch) }
    val consumer = launch { consumeLogs(ch) }

    producer.join()
    consumer.join()
}

Yes, it looks like a “toy.” But it’s a correct toy: it already has a shutdown protocol (close), and there’s no shared mutable collection that two coroutines tug on at the same time.

close in finally: so the consumer doesn’t hang on errors

In practice, the producer can finish not only “as planned.” An exception may be thrown, cancellation may happen, or you may hit return early. If you don’t close the channel in these scenarios, the consumer can hang waiting for new messages, because from its point of view “life isn’t over yet.”

That’s why this is a useful discipline: if your code is responsible for completing the channel, close it in finally. It’s exactly the same idea as with any resource: “clean up no matter what.”

import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.launch
import kotlinx.coroutines.channels.Channel

fun main() = runBlocking {
    val ch = Channel<Int>()

    val producer = launch {
        try {
            repeat(3) { i -> ch.send(i) }
        } finally {
            ch.close()
        }
    }

    for (x in ch) println("x=$x") // x=0, x=1, x=2
    producer.join()
}

Here, even if something unpleasant happens inside try, finally will still run, the channel will be closed, and the for (x in ch) loop will complete correctly.

3. Common mistakes when working with Channel

Mistake #1: you forgot close()—and the program “hangs,” as if it started pondering the meaning of life.
This is the most common scenario: the consumer reads from the channel in a loop, while the producer finished its work and left without closing the channel. The consumer honestly waits for the next element and will wait until the end of time (or until you hit Stop in the IDE). The cure is not magic, but protocol: the channel is closed by whoever “owns the production” of messages, and often this is done in finally.

Mistake #2: trying to call send/receive from a regular function without suspend and without a coroutine context.
send and receive can suspend a coroutine, so Kotlin requires you to be in runBlocking or inside launch/async, or at least in a suspend function called from a coroutine. If you try to “just call it,” the compiler will complain—and this is that rare case where the compiler is not being pedantic, but acting as your bodyguard.

Mistake #3: expecting one Channel to broadcast one message to all consumers.
The intuition “channel = broadcast” is common, but the model is different: each value from a Channel is received by only one receiver. If you have two consumers, they will split messages between them rather than duplicate them. That’s not a bug—it’s the core meaning of a task queue.

Mistake #4: confusing capacity with the “number of receivers.”
Channel capacity affects only buffering: how many elements can temporarily be “put inside” without making the sender wait. It says nothing about how many coroutines will “see” a message. A message is still delivered to exactly one receiver; it’s just that sending can happen either hand-to-hand or with a small buffer cushion.

Mistake #5: closing the channel “wherever,” and then being surprised by exceptions on send.
If you closed the channel and someone then tries to send one more message, that’s already a protocol violation. As a result, you’ll get a crash (or at least very unpleasant behavior). Pick one place where the channel is closed, and make that place logically responsible for completion—otherwise the code turns into a detective story where the murderer is “someone among us.”

Mistake #6: reading the channel via an infinite while (true) { receive() } without a proper completion condition.
Technically you can write it that way, but it almost always makes life harder: you’ll have to separately think about how to exit the loop, what happens on a closed channel, and where to catch an exception. The for (x in ch) loop reads more simply and directly reflects the meaning: “read everything until it ends.”

1
Task
Kotlin SELF, level 54, lesson 0
Locked
The Secret Package
The Secret Package
1
Task
Kotlin SELF, level 54, lesson 0
Locked
Narrow buffer
Narrow buffer
1
Task
Kotlin SELF, level 54, lesson 0
Locked
Sum of packages
Sum of packages
1
Task
Kotlin SELF, level 54, lesson 0
Locked
Closing the gateway
Closing the gateway
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION