Channels
KOTLIN › Concurrency
Kotlin channels for coroutine-to-coroutine communication: send/receive, buffering strategies, produce, select, and when to pick channels over flows.
Channels are the concurrency primitive that lets two coroutines talk to each other through a suspending queue. Interviewers probe channels to see whether you understand hot-vs-cold data, back-pressure, fan-out/fan-in patterns, and the relationship between channels and flows. Expect questions on buffer types, the difference between send and trySend, why channels are hot while flows are cold, and how channelFlow bridges the two. Knowing when to reach for a channel instead of a flow is a strong signal of real-world coroutine experience.
What this covers
- A Channel is a hot, suspending queue: send() suspends when full, receive() suspends when empty
- Four capacity modes: RENDEZVOUS (0), BUFFERED (64), CONFLATED (keeps latest), UNLIMITED
- produce {} is a coroutine builder that returns a ReceiveChannel, closing it on completion
- Fan-out: multiple coroutines receiving from one channel; fan-in: multiple senders into one channel
- Channels are hot (active regardless of consumers); flows are cold (only run when collected)
- channelFlow {} bridges the two: a cold Flow backed by a channel, allowing multi-coroutine emission