Structured concurrency

Roman Elizarov
Sep 12, 2018 · 5 min read

Asynchronous operations

fun requestSomeData() {
launch(UI) {
updateUI(performRequest())
}
}
fun requestSomeData() {
launch {
updateUI(performRequest())
}
}

Parallel decomposition

suspend fun loadAndCombine(name1: String, name2: String): Image { 
val deferred1 = async { loadImage(name1) }
val
deferred2 = async { loadImage(name2) }
return
combineImages(deferred1.await(), deferred2.await())
}
suspend fun loadAndCombine(name1: String, name2: String): Image =
coroutineScope {
val deferred1 = async { loadImage(name1) }
val
deferred2 = async { loadImage(name2) }
combineImages(deferred1.await(), deferred2.await())
}

Further reading

Roman Elizarov

Written by

Team Lead for Kotlin libraries @JetBrains

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade