Building the Kotlin Coroutines Framework from Scratch

By Omkar Tenkale - Android Engineer @Gojek

suspend fun
 
 
 
 
 
Why?
 
 
 
 
 
 
 
 
 
 
1 special function
 
 
 
 
 
 
launch function
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
withContext
 
 
 
suspend fun blocks too!
 
 
 
 
Every statement in the coroutine will always run in same dispatcher
 
Dispatcher != Thread
coroutines do not declare what thread they run on, they declare what dispatcher they run on
Delay
 
 
 
 
 
 
 
Structured Concurrency
 
When a coroutine finishes, it needs to wait for its children
 
 
Cancellation
 
 
 
 
 
 
 
Modifying launch to support cancellation
 
 
 
 

Is it that simple?

Things we did not consider

Race conditions, Deadlocks

Multi language support

Edge cases

Performance optimizations

Flows, sequences, channels, actors, Mutexes, selects

Exception handling

github.com/Kotlin/kotlinx.coroutines

In depth coroutines

In depth coroutines

Linkedin @omkartenkale

Twitter @omkartenkale