A Flow is a cold asynchronous stream of values—like sequences for coroutines. Emit items with flow { } builders; collectors apply map, filter, and terminal operators. Requires kotlinx-coroutines locally; this lesson previews stream thinking with threads.
Cold streams
Each collector runs the flow from the start unless cached with shareIn or stateIn in real projects. Backpressure and cancellation propagate through structured concurrency.
runBlocking with flows (Gradle)
// Local only
runBlocking {
simpleFlow().collect { println(it) }
}
In production services, collect inside a coroutineScope tied to request lifetime—not blocking threads per event.
Important interview questions and answers
- Q: Flow vs Sequence?
A: Sequences are synchronous; flows can suspend between emissions and respect coroutine context and cancellation. - Q: Cold vs hot stream?
A: Cold flows start work per collector; hot streams (SharedFlow, channels) broadcast to multiple subscribers.
Self-check
- What library provides Flow?
- Does each collector restart a cold flow?
Tip: Flow is cold—collecting starts execution; compare with hot StateFlow in Android ViewModel patterns locally.
Interview prep
- Cold vs hot?
Flow is cold until collected; StateFlow is hot state holder.
- Dependency?
kotlinx-coroutines library on Gradle classpath.