Vsevolod Ganin
11/07/2019, 8:17 PMGlobalScope.launch(Dispatchers.Unconfined)
will actually dispatch? I stumbled upon some case when GlobalScope.launch(start = CoroutineStart.UNDISPATCHED, context = Dispatchers.Unconfined)
!= GlobalScope.launch(Dispatchers.Unconfined)
but I can’t quite figure what’s causing itoctylFractal
11/07/2019, 8:20 PMVsevolod Ganin
11/07/2019, 8:24 PMDispatchers.Main.immediate
parent context. Simple test:
GlobalScope.launch(Dispatchers.Main.immediate) {
GlobalScope.launch(Dispatchers.Unconfined) {
println("$coroutineContext: before")
}
println("$coroutineContext: after")
}
It prints:
[CoroutineId(3), "coroutine#3":StandaloneCoroutine{Active}@229841e, Main [immediate]]: after
[CoroutineId(4), "coroutine#4":StandaloneCoroutine{Active}@f2294ff, Unconfined]: before
And if I use start = CoroutineStart.UNDISPATCHED
it works correctly.octylFractal
11/07/2019, 9:28 PMDispatchedContinuation<*>.executeUnconfined
-- when you are already in an event loop (which I believe Dispatchers.Main.immediate counts as well) it will not immediately execute, but re-dispatch for later to avoid stack overflowsCoroutineStart.UNDISPATCHED
is a little more strict and does not use the event loop to avoid stack overflowsVsevolod Ganin
11/07/2019, 10:03 PMDispatchers.Main
yield correct result:
[CoroutineId(29), "coroutine#29":StandaloneCoroutine{Active}@c6ed5c2, Unconfined]: before
[CoroutineId(25), "coroutine#25":StandaloneCoroutine{Active}@65e4fd3, Main]: after
octylFractal
11/07/2019, 10:07 PMVsevolod Ganin
11/07/2019, 10:14 PMMain.immediate
and Unconfined
share the same event loop to avoid stack overflows? Is it not a private implementation detail?octylFractal
11/07/2019, 10:14 PMCoroutineDispatcher.isDispatcherNeeded
returns false, which means that they share it because it is the same mechanismVsevolod Ganin
11/07/2019, 10:21 PM