I wanted to get a customized <Dispatchers.IO> with...
# coroutines
c
I wanted to get a customized Dispatchers.IO with custom number of threads, is it ok and wise to use ExperimentalCoroutineDispatcher().blocking(nThreads) ? @gildor
w
Just create a fixed thread pool and then use it as as displatcher
Executors.newFixedThreadPool(5).toCoroutineDispatcher()
IIRC
c
but that is not optimized for IO isn't it?
w
The IO dispatcher is just a shared pool of threads that get spin up on demand, it shares it's threads with the default dispatcher. So, if you just want a pool of some small number of threads to do work on, the example I posted would be fine
1
The downside would be that I don't think the fixed thread pool would shutdown/spin up threads on demand but I could be wrong
l
You can also configure the upper limit of
<http://Dispatchers.IO|Dispatchers.IO>
which defaults to 64, it's documented.
c
yes I saw that but I wanted to create a parametrized factory to do it so didn't want to deal with properties
w
Problem with the configuration is it would effect all uses of default/IO dispatcher, but maybe not a problem for this case
c
as well...
do you have an idea on how to know how many elements are in a channel at any given moment? I tried count() but it consumes them
in order to count them I might have to just consume them and push them back in
w
yea it always pulls them all when you count
if you need to know how many there are channels may not be what you want
c
uhm, right why not using a deque for example
i just wanted to know if it's full or not to assess how loaded is the worker pool pulling messages from the channel
so i need a concurrent data structure
but maybe there is another way to do what i want to do
basically i want to measure how busy is a worker pool
uhm maybe i could use an atomic counter
rubber duck 3
when i submit the operation to the pool
measuring how full the channel is doesn't anyway tell me much other than "at least 1 thread free" or "all busy"
d
You can use a limiting dispatcher. This is internal coroutines api. You need to cast either default or io dispatcher, I dont remember
(<http://Dispatchers.IO|Dispatchers.IO> as SomeInternalDispatcherType).limitingDispatcher(n)
I think default dispatcher is just limiting dispatcher from Io but not certain, been some time since I looked at it
g
I wanted to get a customized Dispatchers.IO with custom number of threads, is it ok and wise to use
It really depends on your case