Created
May 8, 2021 15:39
-
-
Save utybo/017beb96465c40cd5c0bf3f724f8400e to your computer and use it in GitHub Desktop.
Illustration of event loop in runBlocking
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
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