What is Structured Concurrency in Kotlin: Simple Explanation and Example
structured concurrency means organizing concurrent tasks so they run in a clear, manageable way tied to the program's structure. It ensures that child tasks complete before their parent task finishes, making code easier to read and safer by avoiding leaks or unfinished work.How It Works
Structured concurrency in Kotlin works like a family tree where every child task belongs to a parent task. When you start a new concurrent task inside a parent, Kotlin keeps track of it. The parent waits for all its children to finish before it ends itself.
Think of it like cooking a meal: you start boiling water (parent task), then add pasta and vegetables (child tasks). You don’t finish cooking until both pasta and vegetables are done. This way, you avoid leaving anything half-cooked or forgotten.
This approach helps prevent bugs where tasks run forever or get lost because the program ended too soon. It makes your concurrent code easier to understand and maintain.
Example
This example shows how Kotlin uses structured concurrency with coroutineScope. The parent coroutine waits for both child coroutines to finish before printing the final message.
import kotlinx.coroutines.* fun main() = runBlocking { println("Start parent coroutine") coroutineScope { launch { delay(500L) println("Child coroutine 1 done") } launch { delay(300L) println("Child coroutine 2 done") } } println("All child coroutines completed") }
When to Use
Use structured concurrency whenever you write concurrent code in Kotlin to keep tasks organized and safe. It is especially useful in applications like network calls, file operations, or UI updates where you want to make sure all related tasks finish properly.
For example, in an app fetching data from multiple sources, structured concurrency ensures all data loads before showing the result. It also helps avoid crashes or memory leaks by cleaning up unfinished tasks when the parent task ends.
Key Points
- Structured concurrency ties child tasks to their parent task's lifecycle.
- Parents wait for all children to finish before completing.
- It prevents resource leaks and unfinished tasks.
- Kotlin provides
coroutineScopeandsupervisorScopeto help implement it. - Makes concurrent code easier to read, debug, and maintain.