This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun main() { | |
runBlocking { | |
launch(Dispatchers.Default) { | |
println("Hello1") | |
a() | |
println("Hello2") | |
} | |
println("Hello3") | |
} | |
} | |
suspend fun a() = coroutineScope { | |
println("Hello4") | |
delay(100) | |
println("Hello5") | |
} | |
Output: | |
Hello3 | |
Hello1 | |
Hello4 | |
Hello5 | |
Hello2 | |
// Note how Hello3 is printed before Hello1 since launch isn't blocking | |
// the main thread | |
// | |
// Also note how Hello2 is printed after Hello5 | |
// since coroutineScope suspends the calling f() execution | |
// | |
// It's just that runBlocking will wait for all it's child to | |
// complete before completing itself while | |
// it blocks the thread it was called on. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment