Last active
March 14, 2020 03:10
-
-
Save wickedev/daeaab25b287d14a377f31366d339770 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/** | |
output: | |
main run in [ main ] | |
fetchData run in [ DefaultDispatcher-worker-1 ] | |
runApplication run in [ main ] result: [ Hello Coroutine ] | |
*/ | |
import kotlinx.coroutines.* | |
fun main() = runBlocking { | |
println("main run in [ ${Thread.currentThread().name} ]") | |
runApplication() | |
} | |
suspend fun runApplication() { | |
val result = fetchData() | |
println("runApplication run in [ ${Thread.currentThread().name} ] result: [ $result ]") | |
} | |
suspend fun fetchData() = withContext(Dispatchers.Default) { | |
delay(1_000) | |
println("fetchData run in [ ${Thread.currentThread().name} ]") | |
return@withContext "Hello Coroutine" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment