📖 Scenario: You are building a simple Kotlin program that uses coroutines to perform a task asynchronously. This is useful when you want to run code without blocking the main thread, like loading data or doing background work.
🎯 Goal: Create a Kotlin program that launches a coroutine using the launch coroutine builder to print a message asynchronously.
📋 What You'll Learn
Create a main function
Use runBlocking to start the coroutine scope
Launch a coroutine using launch
Inside the coroutine, print the message "Hello from coroutine!"
Print "Hello from main!" in the main thread
💡 Why This Matters
🌍 Real World
Coroutines help run tasks like network calls or file operations without freezing the app interface.
💼 Career
Understanding coroutine builders like <code>launch</code> is essential for Kotlin developers working on Android or backend applications to write efficient asynchronous code.
Progress0 / 4 steps
1
Create the main function with runBlocking
Write a main function that uses runBlocking to create a coroutine scope.
Kotlin
Hint
Use fun main() = runBlocking { } to start your coroutine scope.
2
Launch a coroutine inside runBlocking
Inside the runBlocking block, launch a coroutine using launch.
Kotlin
Hint
Use launch { } inside runBlocking to start a coroutine.
3
Print a message inside the coroutine
Inside the launch coroutine, write a println statement to print "Hello from coroutine!".
Kotlin
Hint
Use println("Hello from coroutine!") inside the launch block.
4
Print a message in the main thread
After launching the coroutine, write a println statement in the runBlocking block to print "Hello from main!".
Kotlin
Hint
Use println("Hello from main!") after the launch block inside runBlocking.
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
Step 1: Understand the purpose of launch
The launch builder starts a coroutine that runs concurrently without blocking the current thread.
Step 2: Compare with other coroutine builders
Unlike async, launch does not return a result and does not block the thread.
Final Answer:
Starts a new coroutine that runs independently without blocking the current thread. -> Option A
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
Step 1: Recall the syntax of launch
The launch builder requires a lambda block enclosed in curly braces.
Step 2: Check each option
launch { println("Hello") } correctly uses launch { ... }. Others have syntax errors or wrong symbols.
Because of cancellation, the launched coroutine does not complete, so "Done" is not printed, but "Cancelled" is printed after cancel.
Final Answer:
Calling cancel() cancels the runBlocking scope immediately, so "Done" is never printed. -> Option D
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
Step 1: Ensure main waits for coroutines
Using runBlocking with launch inside ensures the main thread waits for all launched coroutines to complete.
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.
Final Answer:
runBlocking with launch inside to wait for all coroutines. -> Option A
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 unknownstrongwhile earning +1 Grip toward your certificate.