Kotlin Coroutines - Detailed Animation Visualizer
Watch step-by-step execution with detailed logging
Cold Flow
IdleEach collector triggers a new, independent flow execution from the beginning. Think of it like a YouTube video - each viewer starts from the beginning.
- Database queries - each UI component gets fresh data
- Network requests - each subscriber triggers its own API call
- File reading operations - each collector reads from start
- User-specific calculations - personalized for each observer
SharedFlow (Hot)
Broadcasts values to all active collectors. New collectors can get recent values from replay buffer. Like a live TV broadcast - you join wherever it currently is.
- Event bus - app-wide events (user login, logout, etc.)
- Live data streams - stock prices, sensor readings
- Chat messages - broadcast to all connected users
- Progress updates - file upload/download progress to multiple UI components
StateFlow
IdleAlways holds one current value. New collectors immediately get the current state. Automatically skips consecutive duplicate values. Perfect for UI state.
- UI state - current screen, theme mode, user preferences
- User authentication state - logged in/out status
- App configuration - settings that components need to read
- Selected items - current selection in lists or navigation
Channel
IdleHot stream for coroutine communication. Each value is consumed by only one receiver. Like a queue where items are removed when received.
โข Rendezvous (capacity = 0): Direct handoff, sender waits for receiver
โข Buffered (capacity > 0): Queue with buffer space
- Task queues - processing background jobs one at a time
- Actor model - sending messages to actors for processing
- Rate limiting - controlling flow of requests or events
- Load balancing - distributing work among multiple workers
withContext
IdleSwitches the coroutine to a different dispatcher/thread and returns result. Essential for performing UI updates on Main thread or IO operations on IO thread.
- Network calls on IO thread, then update UI on Main thread
- Heavy computations on Default thread, preserve UI responsiveness
- File operations on IO, then process results on CPU threads
- Database queries on IO, then update UI components safely
launch { }
IdleFire-and-forget coroutine. Starts a new coroutine and doesn't return a result. Returns a Job that can be used to cancel the coroutine.
- Logging events - fire-and-forget logging operations
- Analytics tracking - send events without waiting for response
- Cache warming - preload data in background
- Background sync - periodic data synchronization
async { }
IdleCreates a coroutine that returns a result. Returns Deferred<T> immediately. Use await() to get the actual result when needed.
- Parallel API calls - fetch multiple data sources simultaneously
- Concurrent computations - process multiple calculations at once
- Parallel file processing - read/parse multiple files concurrently
- Combined operations - wait for multiple async tasks to complete
runBlocking
IdleBlocks the current thread until the coroutine completes. Used in main() functions and tests. Avoid in production code!
- Main function - bridging between blocking and suspending worlds
- Unit tests - testing suspend functions in blocking test frameworks
- Integration tests - waiting for async operations to complete
- Script/utility functions - simple command-line tools