Skip to content

Instantly share code, notes, and snippets.

@utybo
Created May 8, 2021 15:39
Show Gist options
  • Save utybo/017beb96465c40cd5c0bf3f724f8400e to your computer and use it in GitHub Desktop.
Save utybo/017beb96465c40cd5c0bf3f724f8400e to your computer and use it in GitHub Desktop.
Illustration of event loop in runBlocking
import kotlinx.coroutines.*
fun printCurrentThread(prefix: Int) {
println("[$prefix] I'm in: " + Thread.currentThread().name)
}
fun main() {
printCurrentThread(1)
runBlocking {
printCurrentThread(2)
launch {
printCurrentThread(3)
}
printCurrentThread(4)
delay(100) // Just to suspend
printCurrentThread(5)
}
}
/*
Output:
[1] I'm in: main
[2] I'm in: main @coroutine#1
[4] I'm in: main @coroutine#1
[3] I'm in: main @coroutine#2
[5] I'm in: main @coroutine#1
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment