#coroutines
19 snippets tagged with #coroutines
Coroutines — launch, async, and Structured Concurrency
Write concurrent code with Kotlin coroutines: launch, async/await, structured concurrency, and dispatchers.
Best for: Parallel API calls in backend services
Flow — Reactive Streams with Coroutines
Build reactive pipelines with Kotlin Flow: emit, collect, transform, combine, and error handling.
Best for: Streaming data processing pipelines
Channels — Producer-Consumer with Coroutines
Communicate between coroutines with Channels: produce, actor pattern, fan-out/fan-in, and select.
Best for: Producer-consumer patterns in coroutines
Coroutine Mutex and Shared Mutable State
Safely manage shared state across coroutines: Mutex, atomic operations, and actor-based state.
Best for: Thread-safe counters and accumulators
Coroutine Exception Handling and Supervision
Handle errors in coroutines: CoroutineExceptionHandler, supervisorScope, try-catch, and error propagation.
Best for: Robust error handling in concurrent operations
StateFlow and SharedFlow — Hot Streams
Manage state with hot flows: StateFlow for reactive state, SharedFlow for events, and MutableStateFlow.
Best for: Reactive UI state management
Flow Advanced Operators
Advanced flow operations: debounce, flatMapMerge, conflate, buffer, retry, and custom operators.
Best for: Search input debouncing
Coroutine Timeouts and Cancellation
Control coroutine lifecycle: withTimeout, withTimeoutOrNull, isActive checking, and cooperative cancellation.
Best for: API call timeout handling
Structured Concurrency Patterns
Master coroutine structured concurrency: coroutineScope, async/await, fan-out/fan-in, and parallel map.
Best for: Parallel I/O operations with concurrency limits
Coroutine Dispatchers and Context
Choose the right dispatcher: Default, IO, Main, Unconfined, and custom thread pools for coroutines.
Best for: Choosing optimal dispatcher for workload type
Suspend Functions — Basics and Patterns
Understand suspend functions: sequential vs concurrent, suspend composition, and callback wrapping.
Best for: Sequential vs concurrent API calls
SupervisorJob and Error Isolation
Isolate coroutine failures with SupervisorJob: independent child coroutines, partial failure handling.
Best for: Independent failure isolation in microservices
Coroutine Channels Producer Consumer
Use Kotlin channels for coroutine communication: buffered, conflated, fan-out, and fan-in patterns.
Best for: Coroutine-based producer-consumer patterns
Spring WebFlux with Kotlin Coroutines
Build reactive Spring APIs using Kotlin coroutines: suspend functions, Flow, and coRouter DSL.
Best for: Non-blocking REST APIs with Kotlin coroutines
Coroutine Mutex and Synchronization
Synchronize shared mutable state in coroutines with Mutex, atomic operations, and thread confinement.
Best for: Thread-safe shared state in coroutines
Kotlin Flow Operators and Transforms
Master Flow operators: map, filter, combine, zip, debounce, retry, buffer, and custom operators.
Best for: Reactive data streams with transformation
Coroutine Select Expression
Use select expression to await the first result from multiple suspending operations or channels.
Best for: Racing multiple async operations
StateFlow and SharedFlow Usage Patterns
Use hot flows for state management: MutableStateFlow for UI state, SharedFlow for events and broadcasting.
Best for: UI state management in Android/KMP apps
Structured Concurrency Patterns
Apply structured concurrency: parallel decomposition, scoped tasks, cancellation, and resource cleanup.
Best for: Parallel API calls with structured lifecycle