Bird
Raised Fist0
Kotlinprogramming~10 mins

Launch coroutine builder in Kotlin - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Launch coroutine builder
Start main function
↓
Call launch builder
↓
Coroutine starts
↓
Execute coroutine code
↓
Coroutine completes
↓
Main continues or waits
↓
Program ends
The launch builder starts a coroutine that runs concurrently, allowing main to continue or wait for completion.
Execution Sample
Kotlin
fun main() = runBlocking {
    launch {
        println("Hello from coroutine")
    }
    println("Hello from main")
}
Starts a coroutine that prints a message while main also prints its own message.
Execution Table
StepActionEvaluationOutput
1Enter runBlocking scoperunBlocking starts main coroutine
2Call launch builderNew coroutine starts concurrently
3Main prints messagePrints inside main coroutineHello from main
4Coroutine prints messagePrints inside coroutineHello from coroutine
5runBlocking waits for launch coroutineCoroutine completes
6Program endsAll coroutines done
💡 All coroutines complete, runBlocking scope ends, program terminates
Variable Tracker
VariableStartAfter launchAfter coroutine printAfter main printFinal
Coroutine StateNot startedStartedRunningCompletedCompleted
Main StateRunningRunningRunningRunningCompleted
Key Moments - 2 Insights
Why does "Hello from main" sometimes print before "Hello from coroutine"?
Because launch starts a new coroutine concurrently, main continues immediately (see execution_table step 3 before step 4 finishes).
Does launch block the main thread until coroutine finishes?
No, launch starts coroutine asynchronously; runBlocking waits for all coroutines before ending (see execution_table step 5).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, at which step does the coroutine print its message?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Check the Output column for the coroutine's print message.
According to variable_tracker, what is the Coroutine State after launch is called?
AStarted
BNot started
CRunning
DCompleted
💡 Hint
Look at the 'After launch' column for Coroutine State.
If we remove runBlocking, what would happen to the coroutine's output?
AIt will always print before main
BIt might not print at all before program ends
CIt will block main until done
DIt will print twice
💡 Hint
Without runBlocking, main may finish before coroutine runs (see concept_flow).
Concept Snapshot
launch { ... } starts a new coroutine concurrently.
It runs code asynchronously without blocking main.
Use runBlocking to wait for coroutine completion.
Output order may vary due to concurrency.
launch returns immediately, coroutine runs in background.
Full Transcript
This example shows how the launch coroutine builder starts a new coroutine inside a runBlocking scope. The launch coroutine runs concurrently with the main coroutine. The main coroutine continues immediately after launch is called, printing its message. The launched coroutine prints its message asynchronously. The runBlocking scope waits for all coroutines to finish before ending the program. Variables track coroutine and main states from start to completion. Key points include understanding concurrency and that launch does not block main. The visual quiz tests understanding of execution steps and coroutine states.

Practice

(1/5)
1. What does the launch coroutine builder do in Kotlin?
easy
A. Starts a new coroutine that runs independently without blocking the current thread.
B. Blocks the current thread until the coroutine completes.
C. Returns a value from the coroutine immediately.
D. Creates a new thread for each coroutine.

Solution

  1. Step 1: Understand the purpose of launch

    The launch builder starts a coroutine that runs concurrently without blocking the current thread.
  2. Step 2: Compare with other coroutine builders

    Unlike async, launch does not return a result and does not block the thread.
  3. Final Answer:

    Starts a new coroutine that runs independently without blocking the current thread. -> Option A
  4. Quick Check:

    launch = independent coroutine start [OK]
Hint: Remember: launch starts, doesn't block or return value [OK]
Common Mistakes:
  • Thinking launch blocks the thread
  • Expecting launch to return a value
  • Confusing launch with thread creation
2. Which of the following is the correct syntax to launch a coroutine in Kotlin?
easy
A. launch: { println("Hello") }
B. launch { println("Hello") }
C. launch -> println("Hello")
D. launch() println("Hello")

Solution

  1. Step 1: Recall the syntax of launch

    The launch builder requires a lambda block enclosed in curly braces.
  2. Step 2: Check each option

    launch { println("Hello") } correctly uses launch { ... }. Others have syntax errors or wrong symbols.
  3. Final Answer:

    launch { println("Hello") } -> Option B
  4. Quick Check:

    Correct coroutine launch syntax = launch { println("Hello") } [OK]
Hint: Use curly braces {} after launch for the coroutine block [OK]
Common Mistakes:
  • Omitting curly braces
  • Using parentheses without braces
  • Using arrow or colon incorrectly
3. What will be the output of this Kotlin code snippet?
import kotlinx.coroutines.*
fun main() = runBlocking {
    launch {
        delay(100)
        println("World")
    }
    println("Hello")
}
medium
A. World\nHello
B. Hello
C. Hello\nWorld
D. World

Solution

  1. Step 1: Analyze the order of execution

    The launch coroutine delays 100ms before printing "World". Meanwhile, println("Hello") runs immediately.
  2. Step 2: Understand runBlocking behavior

    runBlocking waits for all child coroutines to complete before exiting, so both prints occur.
  3. Final Answer:

    Hello World -> Option C
  4. Quick Check:

    Immediate print then delayed print = Hello\nWorld [OK]
Hint: Immediate prints appear before delayed ones in launch [OK]
Common Mistakes:
  • Assuming launch blocks main thread
  • Expecting 'World' before 'Hello'
  • Ignoring delay effect
4. Identify the error in this Kotlin coroutine code:
fun main() = runBlocking {
    launch {
        delay(100)
        println("Done")
    }
    cancel()
    println("Cancelled")
}
medium
A. println("Cancelled") should be inside launch.
B. delay() cannot be used inside launch.
C. launch must return a value.
D. Calling cancel() cancels the runBlocking scope immediately, so "Done" is never printed.

Solution

  1. Step 1: Understand cancel() effect on runBlocking

    Calling cancel() cancels the entire coroutine scope immediately, stopping child coroutines.
  2. Step 2: Check output consequences

    Because of cancellation, the launched coroutine does not complete, so "Done" is not printed, but "Cancelled" is printed after cancel.
  3. Final Answer:

    Calling cancel() cancels the runBlocking scope immediately, so "Done" is never printed. -> Option D
  4. Quick Check:

    cancel() stops coroutines immediately = Calling cancel() cancels the runBlocking scope immediately, so "Done" is never printed. [OK]
Hint: cancel() stops all coroutines in the scope immediately [OK]
Common Mistakes:
  • Thinking delay() is invalid in launch
  • Expecting launch to return a value
  • Assuming println after cancel won't run
5. You want to launch multiple coroutines that print numbers 1 to 3 with a delay, but ensure the main program waits for all to finish. Which code correctly achieves this?
hard
A. runBlocking { repeat(3) { i -> launch { delay(100L * i) println(i + 1) } } }
B. runBlocking { repeat(3) { i -> launch { delay(100L * i) println(i + 1) } } println("Done") }
C. repeat(3) { i -> launch { delay(100L * i) println(i + 1) } } println("Done")
D. runBlocking { repeat(3) { i -> launch { delay(100L * i) println(i + 1) } } delay(400) }

Solution

  1. Step 1: Ensure main waits for coroutines

    Using runBlocking with launch inside ensures the main thread waits for all launched coroutines to complete.
  2. Step 2: Check each option's behavior

    runBlocking { repeat(3) { i -> launch { delay(100L * i) println(i + 1) } } } launches coroutines inside runBlocking, so main waits. runBlocking { repeat(3) { i -> launch { delay(100L * i) println(i + 1) } } println("Done") } prints "Done" immediately without waiting. repeat(3) { i -> launch { delay(100L * i) println(i + 1) } } println("Done") launches outside runBlocking, so main exits early. runBlocking { repeat(3) { i -> launch { delay(100L * i) println(i + 1) } } delay(400) } uses delay but is less reliable than waiting for coroutines.
  3. Final Answer:

    runBlocking with launch inside to wait for all coroutines. -> Option A
  4. Quick Check:

    runBlocking + launch inside = wait for all [OK]
Hint: Use runBlocking to wait for all launched coroutines [OK]
Common Mistakes:
  • Launching coroutines outside runBlocking
  • Printing before coroutines finish
  • Using delay to wait instead of structured concurrency
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard