Search⌘ K

Hot Channel and Cold Flow

Get an overview of hot channel and cold flow.

We'll cover the following...

Returning to coroutines, the most typical way to create a flow is using a builder, similar to the produce function, called flow.

Kotlin 1.5
val channel = produce {
while (true) {
val x = computeNextValue()
send(x)
}
}
val flow = flow {
while (true) {
val x = computeNextValue()
emit(x)
}
}

These builders are conceptually equivalent, but since the behavior of channels and flow is very different, there are also significant differences between these two functions.

Channel example

Take a look at the example below. Channels are hot, so they immediately start calculating the values. This calculation starts in a separate coroutine. This is why produce needs to be a coroutine builder defined as an extension function on CoroutineScope. The ...