Skip to content

Instantly share code, notes, and snippets.

Last active Apr 4, 2020
What would you like to do?
Run the tasks on a dedicated thread pool using Dispatchers
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.yield
suspend fun suspendTask1() {
println("Start task1 | Thread ${Thread.currentThread()}")
println("End task1 | Thread ${Thread.currentThread()}")
suspend fun suspendTask2() {
println("Start task2 | Thread ${Thread.currentThread()}")
println("End task2 | Thread ${Thread.currentThread()}")
fun main() {
println("Start main")
runBlocking {
launch(Dispatchers.Default) { 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