fun main(args: Array<String>) = runBlocking { // this: CoroutineScope
launch {
delay(200L)
println("Task from runBlocking")
}
coroutineScope { // Creates a new coroutine scope
launch {
delay(500L)
println("Task from nested launch")
}
delay(100L)
println("Task from coroutine scope") // This line will be printed before nested launch
}
println("Coroutine scope is over") // This line is not printed until nested launch completes
}
e
elizarov
10/12/2018, 10:05 AM
Yes. The main coroutine is suspended, waiting for
coroutineScope { ... }
to complete, but the thread is not blocked and can do other activities (you can verify that by launching yet another coroutine at the top that prints a dot every 10ms).
👍 3
d
dave08
10/12/2018, 11:22 AM
If the thread keeps going while the children are suspended without any
join()
, wouldn't main exit while the children are still running?
m
mayojava
10/12/2018, 12:55 PM
nice explanation @elizarov
d
dave08
10/13/2018, 8:23 PM
Now I think I got it! The runBlocking scope just launches everything on its own single thread including the new coroutineScope, is that right? Whereas if GlobalScope were used for launch it would just keep on running in main and finish before the launches... its a bit confusing, since before scopes the default for launch was CommonPool... 🙈