spierce7
08/31/2019, 5:11 PM<http://Dispatchers.IO|Dispatchers.IO>
says:
This dispatcher shares threads with a [Default][Dispatchers.Default] dispatcher, so using
`withContext(<http://Dispatchers.IO|Dispatchers.IO>) { ... }` does not lead to an actual switching to another thread —
typically execution continues in the same thread.*
This behavior seems wrong to me. Isn’t the goal of <http://Dispatchers.IO|Dispatchers.IO>
to keep Dispatchers.Default
threads free. Sharing threads between the dispatchers seems counterintuitive to this goal.Mark Murphy
08/31/2019, 5:23 PMThreadPoolExecutor
in Kotlin/JVM) if you want.
And I wouldn't say that <http://Dispatchers.IO|Dispatchers.IO>
is to keep Dispatchers.Default
threads free. It is to provide a separate dispatcher that is aware of long-running/low-CPU work. Dispatchers.Main
explicitly has a separate thread from `Dispatchers.Default`+`Dispatchers.IO`, because we want Default
and IO
to keep Main
free.spierce7
08/31/2019, 5:53 PMDefault
is constrained by the number of CPU’s by default, meaning it can be meant for CPU intensive work, and IO
by default has a max of 64 threads. By sharing threads between IO
and Default
, isn’t there a situation where I have 10 network requests running on the IO
dispatcher, using a blocking java library, but I can’t perform any work on the main dispatcher because my network requests have settled into my Default
threads and aren’t allowing them to be free?
The natural inclination is for me to offload CPU intensive work to the Default
dispatcher, and offload low-cpu blocking work to the IO
dispatcher. This possible restriction would be terrible for a server environment, and forces me to either 1. Create a new dispatcher for blocking network requests / file accesses, or 2. Don’t use the Default
dispatcher for cpu intensive work, or 3. both 1 and 2.spierce7
08/31/2019, 5:56 PMTolriq
08/31/2019, 6:02 PMspierce7
08/31/2019, 6:06 PMDominaezzz
08/31/2019, 6:07 PMTolriq
08/31/2019, 6:08 PMspierce7
08/31/2019, 6:08 PMTolriq
08/31/2019, 6:09 PMTolriq
08/31/2019, 6:09 PMspierce7
08/31/2019, 6:09 PMspierce7
08/31/2019, 6:10 PMTolriq
08/31/2019, 6:10 PMDominaezzz
08/31/2019, 6:10 PMDefault
and IO
dispatchers sharing a single thread pool.Tolriq
08/31/2019, 6:10 PMTolriq
08/31/2019, 6:14 PMDominaezzz
08/31/2019, 6:14 PMDefault
, the thread will be returned to the shared pool, then IO
will take a thread from the shared pool and run the coroutine on it.
This can be optimised by just leaving the coroutine in the same thread, instead of "releasing" and "acquiring" the thread.Dominaezzz
08/31/2019, 6:16 PMuli
09/01/2019, 11:32 AMuli
09/01/2019, 11:33 AM