Stateflow sharedflow and channels
StateFlow, SharedFlow, and Channels Deep Dive¶
Overview¶
These primitives solve different streaming and coordination problems in coroutine-based systems.
Core Concepts¶
StateFlow¶
- always has a current value
- best for state exposure
- good for UI state models
SharedFlow¶
- hot broadcast stream
- configurable replay/buffering
- good for events/shared emissions
Channel¶
- point-to-point communication primitive
- queue-like semantics
- send/receive model
Mutex¶
- protects shared mutable state cooperatively
- suspends instead of blocking threads
Internal Implementation¶
These primitives differ in ownership and delivery semantics:
- state retention (
StateFlow) - broadcast fan-out (
SharedFlow) - queue handoff (
Channel) - mutual exclusion (
Mutex)
Interviewers often care more about correct use-case matching than API memorization.
JVM / Compiler Behavior¶
All of them are coroutine/runtime constructs, not magical language features. Their behavior emerges from concurrency contracts and buffering rules.
Code Examples¶
private val _state = MutableStateFlow(UiState())
val state: StateFlow<UiState> = _state
private val events = MutableSharedFlow<String>()
val mutex = Mutex()
mutex.withLock {
updateSharedState()
}
Common Interview Questions¶
- Q: StateFlow vs SharedFlow? A: Start from delivery semantics: use StateFlow for durable state, SharedFlow or Channel for transient events, and lifecycle-aware collection to prevent duplicate work.
- Q: Channel vs SharedFlow? A: Start from delivery semantics: use StateFlow for durable state, SharedFlow or Channel for transient events, and lifecycle-aware collection to prevent duplicate work.
- Q: When use
Mutexinstead of synchronized locking? A: Lead with correctness then throughput: choose dispatcher by workload type, keep critical sections small, cap parallelism, and monitor tail latency and queue depth.
Production Considerations¶
Common mistakes:
- using
StateFlowfor one-off UI events - using channels where broadcast behavior is needed
- ignoring replay/buffer semantics
- mutating shared state without synchronization
Performance Insights¶
Hot stream choice affects:
- memory retention
- buffering pressure
- event loss or duplication
- collector coordination cost
Senior-Level Insights¶
Strong engineers choose these primitives based on delivery semantics and lifecycle guarantees, not habit.