harry.singh
06/17/2020, 2:16 PMAdam Powell
06/17/2020, 2:27 PMAndrew
06/17/2020, 3:13 PMharry.singh
06/17/2020, 3:16 PMWhen a coroutine suspends
When a coroutine resumes@Adam Powell What or who decide to perform these actions and when? Is it possible that our coroutine will not be resumed if the thread(say, the coroutine was launched on main thread) is rather busy?
How are you verifying the thread is blocked / not blocked when running only a small number?I tried running coroutines before setting UI file on Android and compared with both cases. The UI was not displayed in the former case but was shown comparatively quickly in the latter one
Andrew
06/17/2020, 3:30 PMAdam Powell
06/17/2020, 3:33 PMsuspendCoroutine
. That produces a Continuation
object, which acts like a callback; when something else calls resume
on it, the ContinuationInterceptor
from the continuation's CoroutineContext
is consulted for how to resume the continuation. Usually this is a CoroutineDispatcher
, which acts like an Executor
. The dispatcher is responsible for making sure continuations resume in the right thread.Dispatchers.Main
refers to a dispatcher that uses a main thread Handler
to resume continuations. It's just like calling <http://Handler.post|Handler.post>
.harry.singh
06/17/2020, 4:52 PMNetworkOnMainThreadException
?Andrew
06/17/2020, 5:15 PMharry.singh
06/17/2020, 5:23 PMAndrew
06/17/2020, 5:29 PMharry.singh
06/17/2020, 5:38 PMWhen a coroutine resumes, it monopolizes that thread until it suspends again. If a coroutine never suspends or does a lot of work between suspend points, the thread it runs on will be occupied for that full time.If a coroutine does not occupy a thread always, but is rather suspended and resumed, why do we need to switch to a different thread like
<http://Dispatchers.IO|Dispatchers.IO>
? I get that running a network call in a main thread coroutine will occupy the main thread for a long time, but still it can be done, right? What I understand right now is that moving over to the IO thread means that we just want to suspend the call on a IO threadAndrew
06/17/2020, 5:46 PMharry.singh
06/17/2020, 5:54 PMAndrew
06/17/2020, 5:57 PMharry.singh
06/17/2020, 6:11 PM