Is is possible to change the parallelism of the De...
# coroutines
i
Is is possible to change the parallelism of the Default dispatcher (if yes, how?) or I'll need to create a dispatcher from a pool with $parallelism number of threads?
g
val myDispatcher = Executors.newFixedThreadPool(parallelism).asCoroutineDispatcher()
i
yes, that's how you create the dispatcher, so it isn't possible to change the parallelism of a dispatcher, is it?
g
Sorry, then I misunderstood your question. What exactly do you want to achieve when you say "change the parallelism"?
i
change the maximum number of running coroutines at the same time, running in parallel
your answer solves my problem, thanks, but I also would like to know if there was a way to change how many threads a Dispatcher can use at most simultaneously
g
This doesn't change default dispatcher, but create new one and this disapatcher is not sharing threads with Default and IO
You can set core pool size using system property which used also for Default disptcher creation https://github.com/Kotlin/kotlinx.coroutines/blob/master/core/kotlinx-coroutines-core/src/scheduling/Tasks.kt#L35
What is your use case for this?
i
Android, reduce by 1 the parallelism to avoid some coroutine using a thread that the Main looper would use
not sure if that's possible but better safe than sorry
and thanks a lot, that was exaclty what I was looking for
g
Not sure what you mean about Main looper thread
i
The Android Main (UI) thread
g
I know what is Main looper, I'm not sure how this related to Default disptcher
Because it has own pool of threads
Also using less than 2 threads may be unsafe
i
I'm planning to use max(cores - 1, 1)
Now that I'm considering it, this may be a little useless
g
If you have only 1 thread it may cause some bugs with deadlocks
i
due to the Main thread being alive at all times right?
g
I would say this is not only useless but dangerous
Default disptcher has nothing to do with Main thread
Default != Main thread dispatcher
There is special dispatcher to schedule execution to main thread
i
I was thinking on a CPU level, like when the scheduler will schedule a thread is that possible for like, 4 coroutines use all 4 cores of a pc a freeze the ui?
that's why I was wanting to limit the max parallelism
g
I don't think that it would be on practice, you should have just huge queue of coroutines that consume all resources of all cores just for dispatching or do some CPU consuming operation on all cores but in this case you can limit using some other approaches, like worker pool
On practice you have much more threads in the system
Also Main thread has higher priority than default threads created by executor
i
hm, I see, and when I think that you may have many background services and processes with many other threads, I agree that this couldn't happen, thanks, now I see that I don't need to worry about that 😅
g
Because the main purpose of Dispatcher is just dispatch non-blocking calls so it's probably not your main problem in terms of performance
i
hm, time to find another target to optimize then