SlideShare a Scribd company logo
Kotlin Coroutines
Flow
Morteza Nedaei
github.com/MortezaNedaei/Kotlin-Coroutines-Workshop
linkedin.com/in/nedaei
Agenda
• History
• What is Flow?
• Stream Types
• Flow Builders
• Flow Design
• Flow Operators
• Flow Constrains
• Channels
• SharedFlow & StateFlow
• SharedFlow Design
• Combine Flows
• Flows CRUD
• Flows Lifecycle
• Convert Flows
• Practice J
History
Channel
Async
Await
Shared
Flow
State
Flow
Flow
Channel
https://arxiv.org/pdf/2211.04986
https://github.com/Kotlin/kotlinx.coroutines/issues/3621
What is Flow?
“An asynchronous data stream that sequentially emits values and
completes normally or with an exception.”
Stream Types
COLD
🥶
🥵
§ flow
§ callbackFlow
§ channelFlow
§ SharedFlow
§ StateFlow
§ Channel
LAZY
MULTICAST
HOT
Flow Builders
• emptyFlow()
• flowOf()
• flow { emit() or emitAll() }
• asFlow()
• callbackFlow {}
• channelFlow {}
• MutableSharedFlow<*>()
• MutableStateFlow<*>(null)
Flow Design
Extension Function Type
Functional Interface
This block is executed
each time by calling emit
Higher Order Function
Flow Design
Flow Terminal Operators
• collect()
• collectLatest() – Conflated
• launchIn(scope) = scope.launch { flow.collect() }
• toList()
• toSet()
• first()
• single()
• fold() – both terminal and transform operator
• reduce() both terminal and transform operator
Flow Transform Operators
• transform()
• transformLatest
• onEach()
• map(), mapNotNull {}
• filter(), filterNotNull {}
• fold(), runningFold()
• scan = runningFold()
• reduce(), runningReduce()
Flow Constraints
1. Context Preservation
Flow encapsulates its own execution context and never propagates or leaks it
downstream
2. Exception Transparency
Flow implementations never catch or handle exceptions that occur in
downstream flows
Channels
Channel is a non-blocking primitive for communication between two or more coroutines
using a sender (via SendChannel) and a receiver (via ReceiveChannel).
Provide a way to transfer a stream of values while Deferred Values are used to transfer
a single value between coroutines.
similar to Java's BlockingQueue, but has suspending operations instead of blocking
ones and can be closed.
Channels
Channel Buffer Strategies
Capacity
§ RENDEZVOUS (Default behavior)
§ UNLIMITED (Int.MAX)
§ CONFLATED (DROP_OLDEST )
§ BUFFERED (Default = 64)
§ SUSPEND
§ DROP_OLDEST (Conflated)
§ DROP_LATEST
Overflow
Strategies
Channel
example:
Gopark
Goready
SharedFlow
1. SharedFlow
2. StateFlow: Customized SharedFlow to hold latest value
Uses Lock to
manage thread
safety
NEVER
COMPLETES
StateFlow
1. Conflation – Drop Oldest: onBufferOverflow = BufferOverflow.DROP_OLDEST
2. distinctUntilChanged()
3. Replay Cache = 1: replay = 1
4. Value property: Holds the current state
It’s a SharedFlow with the following properties:
SharedFlow Design
Combine Flows
• combine()
• zip()
• merge()
Flows CRUD
Show me the code !
Flows Lifecycle
• onStart()
• onEach()
• onCompletion()
Convert Flows
• produceIn()
• shareIn()
• stateIn()
Practice :)
1. What’s the differences between Cold Flows and Hot Flows?
2. What’s the differences between Channels and SharedFlows?
3. What is the time complexity of emit function in SharedFlow ?
4. Are Coroutine Channels hot or cold?
5. What is the Logical structure of the buffer in SharedFlow?
References
• https://elizarov.medium.com/cold-flows-hot-channels-d74769805f9
• https://elizarov.medium.com/shared-flows-broadcast-channels-899b675e805c
• https://elizarov.medium.com/kotlin-flows-and-coroutines-256260fb3bdb
• https://elizarov.medium.com/execution-context-of-kotlin-flows-b8c151c9309b
• https://elizarov.medium.com/exceptions-in-kotlin-flows-b59643c940fb
• https://elizarov.medium.com/callbacks-and-kotlin-flows-2b53aa2525cf
• https://github.com/Kotlin/kotlinx.coroutines/issues/254
• https://github.com/Kotlin/kotlinx.coroutines/issues/285
• https://github.com/Kotlin/kotlinx.coroutines/issues/2680
• https://arxiv.org/pdf/2211.04986
• https://kotlinlang.org/docs/flow.html
• https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow
Elizarov papers
Papers
Kotlin docs
GitHub
Thanks for your
attention
J
Morteza Nedaei
github.com/MortezaNedaei/Kotlin-Coroutines-Workshop
linkedin.com/in/nedaei

More Related Content

PPTX
Unlocking the Power of Kotlin Channels.pptx
PDF
Falling into that State (of) Flow: Easy State Management
PDF
Kotlin Coroutines. Flow is coming
PDF
In-depth analysis of Kotlin Flows
PDF
Operation Flow @ ChicagoRoboto
PPTX
Could Virtual Threads cast away the usage of Kotlin Coroutines
PDF
DroidConEgypt-21-10-2022-Coroutines-AhmedNabil.pdf
PDF
Threading Made Easy! A Busy Developer’s Guide to Kotlin Coroutines
Unlocking the Power of Kotlin Channels.pptx
Falling into that State (of) Flow: Easy State Management
Kotlin Coroutines. Flow is coming
In-depth analysis of Kotlin Flows
Operation Flow @ ChicagoRoboto
Could Virtual Threads cast away the usage of Kotlin Coroutines
DroidConEgypt-21-10-2022-Coroutines-AhmedNabil.pdf
Threading Made Easy! A Busy Developer’s Guide to Kotlin Coroutines

Similar to Coroutines Flow & Channels Workshop Slides (20)

PDF
Structured concurrency with Kotlin Coroutines
PPTX
Design patterns with Kotlin
PPTX
Design patterns with kotlin
PDF
The Three Horse Race
PPTX
A Journey From Callback Hell To Kotlin Coroutines
PPTX
Coroutines in Kotlin
PDF
Coroutines in Kotlin
PPTX
Reactive solutions using java 9 and spring reactor
PDF
Kotlin Coroutines in Practice @ KotlinConf 2018
PDF
Reactive programming with Rxjava
PDF
Streaming all the things with akka streams
PDF
Current State of Coroutines
PPTX
Could Virtual Threads cast away the usage of Kotlin Coroutines - DevoxxUK2025
PDF
Reactive Streams: Handling Data-Flow the Reactive Way
PDF
Springone2gx 2014 Reactive Streams and Reactor
PDF
Journey into Reactive Streams and Akka Streams
PDF
Akka Streams and HTTP
PPTX
Design patterns with kotlin
PDF
What is new in java 8 concurrency
PDF
Akka streams - Umeå java usergroup
Structured concurrency with Kotlin Coroutines
Design patterns with Kotlin
Design patterns with kotlin
The Three Horse Race
A Journey From Callback Hell To Kotlin Coroutines
Coroutines in Kotlin
Coroutines in Kotlin
Reactive solutions using java 9 and spring reactor
Kotlin Coroutines in Practice @ KotlinConf 2018
Reactive programming with Rxjava
Streaming all the things with akka streams
Current State of Coroutines
Could Virtual Threads cast away the usage of Kotlin Coroutines - DevoxxUK2025
Reactive Streams: Handling Data-Flow the Reactive Way
Springone2gx 2014 Reactive Streams and Reactor
Journey into Reactive Streams and Akka Streams
Akka Streams and HTTP
Design patterns with kotlin
What is new in java 8 concurrency
Akka streams - Umeå java usergroup
Ad

Recently uploaded (20)

PPTX
Construction Project Organization Group 2.pptx
PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
DOCX
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
PDF
Human-AI Collaboration: Balancing Agentic AI and Autonomy in Hybrid Systems
PDF
Well-logging-methods_new................
DOCX
573137875-Attendance-Management-System-original
PPTX
CH1 Production IntroductoryConcepts.pptx
PDF
BIO-INSPIRED HORMONAL MODULATION AND ADAPTIVE ORCHESTRATION IN S-AI-GPT
PPTX
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
PPT
Project quality management in manufacturing
PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
PPT
Mechanical Engineering MATERIALS Selection
PDF
Model Code of Practice - Construction Work - 21102022 .pdf
PDF
Operating System & Kernel Study Guide-1 - converted.pdf
PPTX
Foundation to blockchain - A guide to Blockchain Tech
PPTX
bas. eng. economics group 4 presentation 1.pptx
PDF
737-MAX_SRG.pdf student reference guides
PPTX
UNIT 4 Total Quality Management .pptx
PDF
PREDICTION OF DIABETES FROM ELECTRONIC HEALTH RECORDS
Construction Project Organization Group 2.pptx
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
Human-AI Collaboration: Balancing Agentic AI and Autonomy in Hybrid Systems
Well-logging-methods_new................
573137875-Attendance-Management-System-original
CH1 Production IntroductoryConcepts.pptx
BIO-INSPIRED HORMONAL MODULATION AND ADAPTIVE ORCHESTRATION IN S-AI-GPT
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
Embodied AI: Ushering in the Next Era of Intelligent Systems
Project quality management in manufacturing
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
Mechanical Engineering MATERIALS Selection
Model Code of Practice - Construction Work - 21102022 .pdf
Operating System & Kernel Study Guide-1 - converted.pdf
Foundation to blockchain - A guide to Blockchain Tech
bas. eng. economics group 4 presentation 1.pptx
737-MAX_SRG.pdf student reference guides
UNIT 4 Total Quality Management .pptx
PREDICTION OF DIABETES FROM ELECTRONIC HEALTH RECORDS
Ad

Coroutines Flow & Channels Workshop Slides