elizarov
09/11/2017, 12:06 PMEmptyCoroutineContext
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)deviant
09/11/2017, 12:10 PMlaunch(EmptyCoroutineContext)
in the ui thread with same result i call launch(UI)
?elizarov
09/11/2017, 1:48 PMlaunch(EmptyCoroutineContext)
or launch(Unconfined)
in UI thread, then after the first async operation like delay
you’ll resume in some other thread and will not be in UI thread anymore. However, with launch(UI)
you always resume in UI thread, guaranteed. It is explained with examples in this section of the guide: https://github.com/Kotlin/kotlinx.coroutines/blob/master/coroutines-guide.md#unconfined-vs-confined-dispatcherdeviant
09/11/2017, 1:53 PMdelay
coroutine? because i can't reproduce 'thread switching' with async(CommonPool){...}.await()
launch(Unconfined){
logThread("coroutine start")
async(CommonPool){
logThread("common pool")
}.await()
logThread("coroutine end")
}
16:53:27.584: coroutine start [thread:1]
16:53:27.591: common pool [thread:11]
16:53:27.595: coroutine end [thread:1]
elizarov
09/11/2017, 2:30 PMasync(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.deviant
09/11/2017, 3:15 PMThread.sleep()
inside of async
now it jumps to another thread. thanks for clarificationelizarov
09/11/2017, 3:17 PMasync(ctx) { ... }.await()
is the same as run(ctx) { ... }
. I’d recommend to always use the later, since it makes your intent to switch to another context more explicit and I don’t have to look at the last line with await
to see what was your intent of using async
.