Unconfined

This dispatcher will execute the coroutine in the current thread until the first suspension point is reached. After suspension, the coroutine will be resumed in whichever thread is used by the suspending computation invoked by it when suspending.

For example, if your unconfined function suspend a() calls suspend b(), which is executed with a dispatcher in  a specific thread, a() will be resumed in the same thread that b() was executed. This happens because of the way in which suspending computations are compiled, and we will explain that in chapter 9, The Internals of Concurrency in Kotlin.

Here is an example of its usage:

fun main(args: Array<String>) = runBlocking {
launch(Unconfined) {
println("Starting in ${Thread.currentThread().name}")
delay(500)
println("Resuming in ${Thread.currentThread().name}")
}.join()
}

This will print an output similar to this. Notice that initially it was running in main(), but then it moved to DefaultExecutor:

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

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