Single thread context

This dispatcher guarantees that, at all times, the coroutine is executed in a specific thread. To create a dispatcher of this type you have to use newSingleThreadContext():

fun main(args: Array<String>) = runBlocking {
val dispatcher = newSingleThreadContext("myThread")

launch(dispatcher) {
println("Starting in ${Thread.currentThread().name}")
delay(500)
println("Resuming in ${Thread.currentThread().name}")
}.join()
}

This will always execute in that same thread, even after any suspensions:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.138.204.186