0
0
Kotlinprogramming~10 mins

Coroutine scope and structured concurrency in Kotlin - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Coroutine scope and structured concurrency
Start Coroutine Scope
Launch Child Coroutines
Child Coroutines Run Concurrently
Wait for All Children to Complete
Coroutine Scope Ends
Continue with Rest of Program
This flow shows how a coroutine scope launches child coroutines, waits for all to finish, then ends, ensuring structured concurrency.
Execution Sample
Kotlin
fun main() = runBlocking {
  launch { delay(100); println("First") }
  launch { delay(50); println("Second") }
  println("Start")
}
This code runs two child coroutines inside a scope, printing messages with delays, showing structured concurrency.
Execution Table
StepActionCoroutineDelay (ms)OutputState
1Enter runBlocking scopemain0Scope started, waiting for children
2Launch first coroutinefirst100First coroutine started, waiting 100ms
3Launch second coroutinesecond50Second coroutine started, waiting 50ms
4Print in main coroutinemain0StartPrinted 'Start' immediately
5Second coroutine delay endssecond50SecondPrinted 'Second' after 50ms
6First coroutine delay endsfirst100FirstPrinted 'First' after 100ms
7All child coroutines completemain0Scope ends, main continues
8Program endsmain0runBlocking returns, program finishes
💡 All child coroutines finished, so runBlocking scope ends and program continues.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 5After Step 6Final
Scope Activefalsetruetruetruetruefalse
First Coroutine Statenot startedstartedstartedstartedcompletedcompleted
Second Coroutine Statenot startednot startedstartedcompletedcompletedcompleted
Output PrintedStartStart, SecondStart, Second, FirstStart, Second, First
Key Moments - 3 Insights
Why does the program wait for both child coroutines before finishing?
Because runBlocking creates a coroutine scope that waits for all launched child coroutines to complete before it ends, as shown in steps 7 and 8.
Why is "Start" printed before "Second" and "First"?
"Start" is printed immediately in the main coroutine without delay (step 4), while the child coroutines print after their delays (steps 5 and 6).
What happens if a child coroutine throws an error inside the scope?
The coroutine scope cancels all other child coroutines and the error propagates, ensuring structured concurrency and no orphan coroutines.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is printed at step 5?
A"Second"
B"First"
C"Start"
DNothing
💡 Hint
Check the 'Output' column at step 5 in the execution_table.
At which step does the coroutine scope end?
AStep 6
BStep 4
CStep 7
DStep 8
💡 Hint
Look for the step where 'Scope ends' is mentioned in the 'State' column.
If the delay in the first coroutine was changed to 30ms, when would "First" print relative to "Second"?
A"First" and "Second" would print at the same time
B"First" would print before "Second"
C"Second" would still print before "First"
D"First" would never print
💡 Hint
Compare delays in the 'Delay (ms)' column and their effect on output timing.
Concept Snapshot
Coroutine scope creates a block where child coroutines run.
All child coroutines must finish before the scope ends.
runBlocking is a coroutine scope that blocks the thread until done.
Structured concurrency prevents orphan coroutines and manages errors.
Use launch inside a scope to start child coroutines.
Delays simulate work and show concurrency timing.
Full Transcript
This example shows how a coroutine scope manages child coroutines. The main function uses runBlocking to create a scope. Inside, two child coroutines launch with different delays. The main coroutine prints "Start" immediately. The second coroutine prints "Second" after 50 milliseconds. The first coroutine prints "First" after 100 milliseconds. The runBlocking scope waits for both child coroutines to finish before ending. This ensures structured concurrency, meaning no child coroutine is left running outside the scope. If any child coroutine fails, the scope cancels others to keep the program safe. This way, coroutines are managed cleanly and predictably.