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

streetsofboston

12/12/2018, 8:55 PM
Question about CoroutineDispatchers and resuming suspension points: Say a coroutine has a context with the
<http://Dispatchers.IO|Dispatchers.IO>
which is backed by a pool of threads, let’s say “Thread-1” through “Thread-64". When a suspension point in a suspend function is reached while running in “Thread-4”, does the function always resume in the same thread, “Thread-4", or could it resume in any of the other threads of the thread-pool of
<http://Dispatchers.IO|Dispatchers.IO>
(it could be “Thread-35”)? I think it is the last option (could be in the other threads of the dispatcher), but I’m not sure … 🙂
d

danny

12/12/2018, 9:25 PM
The class documentation for
kotlinx.coroutines.scheduling.CoroutineScheduler
goes into that a little - looks to me that the transition from non-blocking to blocking would always retain affinity, but the task would be stolen and executed on another thread if it couldn't regain a CPU permit in within
WORK_STEALING_TIME_RESOLUTION_NS
s

streetsofboston

12/12/2018, 9:26 PM
Thanks. This would mean the continuation tries to keep it in the same thread as much as possible, but you should never ever rely on it. Is this correct?
d

danny

12/12/2018, 9:29 PM
Yep
8 Views