Hello, could you help me understand what’s the different between the below codes. Thanks. What’s the fundamental different between using scope.launch and launch inside a launch?
fun main() {
val scope = CoroutineScope(CoroutineName(“myscope”) + Dispatchers.IO)
runBlocking{
val job = scope.launch {
scope.launch { // <----
delay(1000L)
println(“task1”)
}
scope.launch {
delay(900L)
println(“task2”)
}
}
job.join()
}
}
VS
fun main() {
val scope = CoroutineScope(CoroutineName(“myscope”) + Dispatchers.IO)
runBlocking{
val job = scope.launch {
launch { // <----
delay(1000L)
println(“task1”)
}
launch {
delay(900L)
println(“task2”)
}
}
job.join()
}
}
The first one will not print anything but the second one prints
"
task2
task1
"