# Structured Concurrency Anniversary

DevFeed: [Structured Concurrency Anniversary](<https://devfeed.tech/articles/structured-concurrency-anniversary-26028.md>)

Original publisher: [Read original article](<https://elizarov.medium.com/structured-concurrency-anniversary-f2cc748b2401?source=rss-4762e889f8fc------2>)

Author: Roman Elizarov

Published: 2019-09-28T15:02:02Z

Content type: article

Language: en

Sources: [Roman Elizarov](<https://devfeed.tech/sources/stories-by-roman-elizarov-on-medium.md>)

Topics: [Structured concurrency](<https://devfeed.tech/topics/structured-concurrency.md>), [Coroutines](<https://devfeed.tech/topics/coroutines.md>), [Kotlin](<https://devfeed.tech/topics/kotlin.md>), [coroutines-flow](<https://devfeed.tech/topics/coroutines-flow.md>), [Concurrency](<https://devfeed.tech/topics/concurrency.md>), [reactive](<https://devfeed.tech/topics/reactive.md>), [Reactive Streams](<https://devfeed.tech/topics/reactive-streams.md>)

Tags: [1-3](<https://devfeed.tech/tags/1-3.md>), [android](<https://devfeed.tech/tags/android.md>), [concurrency](<https://devfeed.tech/tags/concurrency.md>), [coroutine](<https://devfeed.tech/tags/coroutine.md>), [kotlin](<https://devfeed.tech/tags/kotlin.md>), [kotlin-coroutines](<https://devfeed.tech/tags/kotlin-coroutines.md>), [programming](<https://devfeed.tech/tags/programming.md>), [reactive-streams](<https://devfeed.tech/tags/reactive-streams.md>), [structured-concurrency](<https://devfeed.tech/tags/structured-concurrency.md>)

## AI overview

Kotlin's Structured Concurrency was designed to keep coroutines and their exceptions within a defined lifetime. `coroutineScope { ... }` bounds concurrent work to the current call, while Android's `lifecycleScope` and `viewModelScope` connect coroutines to lifecycle-bound objects. For Flow, `launchIn(scope)` runs collection in a chosen scope so subscriptions need not outlive closing widgets. A legacy caveat is that coroutine builders combine the scope context with an explicit context: passing another `Job` can have unintuitive behavior, so the author advises against it. Flow's `flowOn` avoids this issue by disallowing job replacement. The text also notes that restricting a request to both its own lifetime and a client's scope is currently cumbersome.

## Source excerpt

Photo by Annie Spratt on Unsplash A little over a year ago I announced big conceptual shift in the design of Kotlin Coroutines called Structured Concurrency. From that moment on, it took our team about a month to make the first stable 1.0.0 release of kotlinx.coroutines library. After a year of further work, kotlinx.coroutines had added stable support for cold flows that integrate nicely with reactive streams. The library had reached version 1.3.2 by now. It is good time to look back and see how it all worked out -- what was great, what could be improved. Structured Concurrency accomplished more than we hoped for. Originally, the design of structured concurrency was based on the woes experienced by backend developers trying to implement all sorts of asynchronous and concurrent logic. It was focused on making sure that you cannot ever lose a running coroutine or an exception. The key building block we added back then is coroutineScope { ... } function, which encapsulates concurrent operations and limits their scope to the scope of the current call. There was not much else to it, so our recommendation to UI developers was to implement CoroutineScope interface in various "closeable" entities of their applications. We envisioned a simple picture with a simple scope hierarchy. It turned out to be more elaborate in practice. Structured Concurrency was rapidly adopted by Android, which has quite complicated life-cycles. Android libraries added extensions like lifecycleScope and viewModelScope, enabling concise and safe integration of coroutines with those concepts. It became apparent that code looks clearer when an object encapsulating the scope is separate from the rest of code. Introductory Android Codelab on Coroutines recommends defining coroutine scope like this: private val scope = CoroutineScope(...) Nowadays, this style increasingly looks more appealing, so it's time to adjust our documentation to reflect it. At the same time, structured concurrency laid a solid fou