kevin.cianfarini
08/19/2023, 3:30 PMKirill Zhukov
08/24/2023, 6:05 PMA new single threaded dispatcher has a dedicated thread.Is this still true even if a dispatcher is “created” as a top level field?
val Dispatchers.DatabaseIO: CoroutineContext
get() = databaseIO + CoroutineName("DatabaseIO")
private val databaseIO: CoroutineDispatcher = Dispatchers.IO.limitedParallelism(1)
kevin.cianfarini
08/24/2023, 6:08 PMlimitedParallelism
. I’m talking about using newSingleThreadedContext
https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/new-single-thread-context.htmlKirill Zhukov
08/24/2023, 6:29 PMLimited parallelism doesn’t guarantee you a reserved thread like the other approach does.
kevin.cianfarini
08/24/2023, 6:39 PMlimitedParallelism
will maintain it’s own internal n
worker queues which then delegate to the underlying dispatcher for running. https://github.com/Kotlin/kotlinx.coroutines/blob/master/kotlinx-coroutines-core/common/src/internal/LimitedDispatcher.kt#L47-L51
This does not guarantee that tasks are running on the same thread; this guarantees that no more than n
tasks will be dispatched to the wrapped dispatcher in parallel.kevin.cianfarini
08/24/2023, 6:44 PM