Skip to content

Instantly share code, notes, and snippets.

@wickedev
Last active March 14, 2020 03:10
Show Gist options
  • Save wickedev/daeaab25b287d14a377f31366d339770 to your computer and use it in GitHub Desktop.
Save wickedev/daeaab25b287d14a377f31366d339770 to your computer and use it in GitHub Desktop.
/**
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