launch
Relationships
- The coroutine context is inherited from a CoroutineScope.
Notes
Uncaught exceptions in this coroutine cancel the parent job in the context by default (unless CoroutineExceptionHandler is explicitly specified), which means that when launch is used with the context of another coroutine, then any uncaught exception leads to the cancellation of the parent coroutine. - kotlin doc
TODO: understand exception handling better for co-routines.
Example
package com.glassthought.sandbox
import gt.sandbox.util.output.Out
import kotlinx.coroutines.*
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
val out = Out.standard()
suspend fun slowFunc() {
delay(1000)
out.println("Delayed for 1 second now I am done.")
}
fun main() = runBlocking {
val jobs = (1..10).map {
launch {
out.println("Launching job $it")
slowFunc()
}
}
jobs.forEach { it.join() } // Wait for all coroutines to complete
out.println("End of main")
}
Output and commit to reproduce
Command to reproduce:
gt.sandbox.checkout.commit e152878920ba2f187c97 \
&& cd "${GT_SANDBOX_REPO}" \
&& cmd.run.announce "./gradlew run"
Recorded output of command:
> Task :app:checkKotlinGradlePluginConfigurationErrors SKIPPED
> Task :app:processResources NO-SOURCE
> Task :app:compileKotlin
> Task :app:compileJava NO-SOURCE
> Task :app:classes UP-TO-DATE
> Task :app:run
[2024-11-11T04:59:00.833319Z][elapsed-since-start: 43ms][tname:main/tid:1] Launching job 1
[2024-11-11T04:59:00.852240Z][elapsed-since-start: 53ms][tname:main/tid:1] Launching job 2
[2024-11-11T04:59:00.852370Z][elapsed-since-start: 53ms][tname:main/tid:1] Launching job 3
[2024-11-11T04:59:00.852457Z][elapsed-since-start: 53ms][tname:main/tid:1] Launching job 4
[2024-11-11T04:59:00.852540Z][elapsed-since-start: 53ms][tname:main/tid:1] Launching job 5
[2024-11-11T04:59:00.852624Z][elapsed-since-start: 53ms][tname:main/tid:1] Launching job 6
[2024-11-11T04:59:00.852706Z][elapsed-since-start: 53ms][tname:main/tid:1] Launching job 7
[2024-11-11T04:59:00.852795Z][elapsed-since-start: 53ms][tname:main/tid:1] Launching job 8
[2024-11-11T04:59:00.852890Z][elapsed-since-start: 53ms][tname:main/tid:1] Launching job 9
[2024-11-11T04:59:00.852975Z][elapsed-since-start: 53ms][tname:main/tid:1] Launching job 10
[2024-11-11T04:59:01.856198Z][elapsed-since-start: 1057ms][tname:main/tid:1] Delayed for 1 second now I am done.
[2024-11-11T04:59:01.858536Z][elapsed-since-start: 1059ms][tname:main/tid:1] Delayed for 1 second now I am done.
[2024-11-11T04:59:01.858877Z][elapsed-since-start: 1059ms][tname:main/tid:1] Delayed for 1 second now I am done.
[2024-11-11T04:59:01.859034Z][elapsed-since-start: 1060ms][tname:main/tid:1] Delayed for 1 second now I am done.
[2024-11-11T04:59:01.859168Z][elapsed-since-start: 1060ms][tname:main/tid:1] Delayed for 1 second now I am done.
[2024-11-11T04:59:01.859369Z][elapsed-since-start: 1060ms][tname:main/tid:1] Delayed for 1 second now I am done.
[2024-11-11T04:59:01.859499Z][elapsed-since-start: 1060ms][tname:main/tid:1] Delayed for 1 second now I am done.
[2024-11-11T04:59:01.859628Z][elapsed-since-start: 1060ms][tname:main/tid:1] Delayed for 1 second now I am done.
[2024-11-11T04:59:01.859765Z][elapsed-since-start: 1060ms][tname:main/tid:1] Delayed for 1 second now I am done.
[2024-11-11T04:59:01.859945Z][elapsed-since-start: 1060ms][tname:main/tid:1] Delayed for 1 second now I am done.
[2024-11-11T04:59:01.861177Z][elapsed-since-start: 1062ms][tname:main/tid:1] End of main
BUILD SUCCESSFUL in 1s
2 actionable tasks: 2 executed
Highlighted:
Notice: how delay
is used instead of Thread.sleep
. See why by looking at CoRoutines do-not-play-nice-with Thread.sleep.
Children