This is the example: ``` fun main(args: Array<S...
# coroutines
m
This is the example:
Copy code
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
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
If the thread keeps going while the children are suspended without any
join()
, wouldn't main exit while the children are still running?
m
nice explanation @elizarov
d
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... 🙈