July 2018
Intermediate to advanced
266 pages
7h 11m
English
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:

Read now
Unlock full access