CodeBlanks Logo Icon CodeBlanks

launch

Learn to start coroutines with GlobalScope.launch.

launch

launch starts a new coroutine inside a coroutine scope. It works fire-and-forget and returns no result. Example: runBlocking { launch { println("Ran") } }.

launch returns a Job; you can call join() to wait for it. Thanks to structured concurrency runBlocking does not return until all inner launch calls finish. This means coroutines complete even without an explicit join call.

launch and join

A launch block runs sequentially in its own coroutine. The println lines inside appear in the order written. Example: launch { println("First"); println("Second") } gives deterministic output.

Multiple launch calls can start at once; their order is nondeterministic. For deterministic output use sequential println inside a single launch. join() waits for a coroutine to finish; this helps when ordering matters. delay can also affect ordering.

Practice Write two lines with launch

Add import kotlinx.coroutines.*. Write fun main() = runBlocking; inside call launch { println("First"); println("Second") }. A single launch with sequential writes gives deterministic output.

Starter code

Main.kt
import kotlinx.coroutines.*

// Write two lines with launch inside runBlocking

Sample solution

Main.kt
import kotlinx.coroutines.*

fun main() = runBlocking {
    launch {
        println("First")
        println("Second")
    }
}

Expected output

First
Second

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 Complete the launch call

Question
runBlocking {
    ____ { println("Started") }
}
Answer
runBlocking {
    launch { println("Started") }
}

Explanation: launch starts a new coroutine.

2 Match the launch concepts

Term Meaning
launch Start
Job Return
join() Wait
fire-and-forget No result

3 What does launch return?

  • A value
  • Job Correct answer
  • Unit
  • Deferred

Explanation: launch returns a Job; you can wait with join().

All lessons in Intermediate