F
F
foonfyrick2021-03-30 06:47:57
Kotlin
foonfyrick, 2021-03-30 06:47:57

Dispatcher, Default and IO?

Text from the article:
You can see how Dispatchers.Default is initialized in the createDefaultDispatcher method. The default is DefaultScheduler. If you look at the implementation of Dispatchers.IO, it also uses the DefaultScheduler and allows you to create at least 64 threads on demand. Dispatchers.Default and Dispatchers.IO are implicitly related to each other because they use the same thread pool.

Here is the function

internal actual fun createDefaultDispatcher(): CoroutineDispatcher =
    if (useCoroutinesScheduler) DefaultScheduler else CommonPool

1) What is useCoroutinesScheduler, I see that it is some kind of systemProp, but this does not mean anything, when creating a CoroutineScope(Dispatchers.Default) coroutine, which of these affects the choice, will DefaultScheduler or CommonPool be used?
2) The DefaultScheduler is used by default. If you look at the implementation of Dispatchers.IO, it also uses the DefaultScheduler and allows at least 64 threads to be created on demand. Dispatchers.Default and Dispatchers.IO are implicitly related to each other because they use the same thread pool.

That is, if Dispatchers.IO uses a minimum of 64 threads (and for some reason they didn’t write how many are the maximum), and how many are the minimum using Dispatchers.Default, or the line "Dispatchers.Default and Dispatchers.IO are implicitly related to each other, since they use the same thread pool." implies they both use a minimum of 64 threads?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Zagaevsky, 2021-03-30
@foonfyrick

If I were you, I would stop reading this article and start reading the doc. These are strange conjectures, and immersion in absolutely superfluous details (you can immerse yourself in which, if you wish, yourself, there is a code, I don’t want to look).
Dispatchers.IO

The CoroutineDispatcher that is designed for offloading blocking IO tasks to a shared pool of threads.
Additional threads in this pool are created and are shutdown on demand. The number of threads used by tasks in this dispatcher is limited by the value of “kotlinx.coroutines.io.parallelism” (IO_PARALLELISM_PROPERTY_NAME) system property. It defaults to the limit of 64 threads or the number of cores (whichever is larger).
Moreover, the maximum configurable number of threads is capped by the kotlinx.coroutines.scheduler.max.pool.size system property. If you need a higher number of parallel threads, you should use a custom dispatcher backed by your own thread pool.

That is, the threads in the pool max(64, number_cores).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question