A channel flow is a special cold flow built with channelFlow for concurrent emissions from multiple coroutines. Instead of emit, launched coroutines use send; collection still delivers values sequentially. section 16.2.5
Send values concurrently
val randomNumbers = channelFlow { repeat(10) { launch { send(getRandomNumber()) } } } The builder supplies a coroutine scope, so each launch can execute getRandomNumber concurrently. With ten 500-millisecond delays, the work finishes in roughly 500 milliseconds rather than running ten times sequentially. section 16.2.5
Choose channelFlow when concurrent producers are needed; otherwise prefer the simpler regular cold flow.
- A regular cold flow restricts emit calls to one coroutine; launching background coroutines that emit directly violates the flow invariant. section 16.2.5
- Regular cold flows remain the simplest and most performant option when sequential production is sufficient, because channel flows add concurrency and overhead. section 16.2.5
