Skip to content

Instantly share code, notes, and snippets.

@GauravChaddha1996
Created January 30, 2021 17:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GauravChaddha1996/919b7f14a7807d678232a3ccbd6aeee5 to your computer and use it in GitHub Desktop.
Save GauravChaddha1996/919b7f14a7807d678232a3ccbd6aeee5 to your computer and use it in GitHub Desktop.
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