Skip to content

Instantly share code, notes, and snippets.

@magdamiu
Last active April 4, 2020 18:02
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 magdamiu/28a7fe4816fab3b57127d710324e438c to your computer and use it in GitHub Desktop.
Save magdamiu/28a7fe4816fab3b57127d710324e438c to your computer and use it in GitHub Desktop.
suspend functions with yield
import kotlinx.coroutines.yield
suspend fun suspendTask1() {
println("Start task1 | Thread ${Thread.currentThread()}")
yield()
println("End task1 | Thread ${Thread.currentThread()}")
}
suspend fun suspendTask2() {
println("Start task2 | Thread ${Thread.currentThread()}")
yield()
println("End task2 | Thread ${Thread.currentThread()}")
}
fun main() {
println("Start main")
runBlocking {
launch { suspendTask1() }
launch { suspendTask2() }
println("Called task1 and task2 from ${Thread.currentThread()}")
}
println("End main")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment