https://kotlinlang.org logo
#coroutines
Title
# coroutines
e

elizarov

09/11/2017, 12:06 PM
@deviant
EmptyCoroutineContext
and
Unconfined
are virtually the same with the exception that
Unconfined
is designed to work with debugging facilities of
kotlinx.coroutines
(in debug mode you’ll get the id of the executing coroutine added to the thread name — wastes a lot of CPU, but simplifies debugging)
d

deviant

09/11/2017, 12:10 PM
thanks, Roman. do i understand correct that i can use
launch(EmptyCoroutineContext)
in the ui thread with same result i call
launch(UI)
?
is it valid only for
delay
coroutine? because i can't reproduce 'thread switching' with
async(CommonPool){...}.await()
Copy code
launch(Unconfined){
        logThread("coroutine start")
        async(CommonPool){
            logThread("common pool")
        }.await()
        logThread("coroutine end")

    }
Copy code
16:53:27.584: coroutine start [thread:1]
16:53:27.591: common pool [thread:11]
16:53:27.595: coroutine end [thread:1]
e

elizarov

09/11/2017, 2:30 PM
It is true with any suspending function. In your example, I think, the
async(CommonPool)
coroutine just finishes too fast, so that
await
does not suspend and stays in the same thread. Just spin there for while to see how
Unconfined
coroutine can jump threads.
👍 1
d

deviant

09/11/2017, 3:15 PM
wow. with
Thread.sleep()
inside of
async
now it jumps to another thread. thanks for clarification
5 Views