https://kotlinlang.org logo
#coroutines
Title
# coroutines
i

Icaro Temponi

03/22/2019, 4:37 PM
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

gregorbg

03/22/2019, 4:41 PM
val myDispatcher = Executors.newFixedThreadPool(parallelism).asCoroutineDispatcher()
i

Icaro Temponi

03/22/2019, 4:46 PM
yes, that's how you create the dispatcher, so it isn't possible to change the parallelism of a dispatcher, is it?
g

gregorbg

03/22/2019, 4:48 PM
Sorry, then I misunderstood your question. What exactly do you want to achieve when you say "change the parallelism"?
i

Icaro Temponi

03/22/2019, 4:48 PM
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

gildor

03/22/2019, 4:57 PM
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

Icaro Temponi

03/22/2019, 5:01 PM
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

gildor

03/22/2019, 5:02 PM
Not sure what you mean about Main looper thread
i

Icaro Temponi

03/22/2019, 5:03 PM
The Android Main (UI) thread
g

gildor

03/22/2019, 5:03 PM
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

Icaro Temponi

03/22/2019, 5:04 PM
I'm planning to use max(cores - 1, 1)
Now that I'm considering it, this may be a little useless
g

gildor

03/22/2019, 5:05 PM
If you have only 1 thread it may cause some bugs with deadlocks
i

Icaro Temponi

03/22/2019, 5:05 PM
due to the Main thread being alive at all times right?
g

gildor

03/22/2019, 5:05 PM
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

Icaro Temponi

03/22/2019, 5:07 PM
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

gildor

03/22/2019, 5:12 PM
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

Icaro Temponi

03/22/2019, 5:14 PM
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

gildor

03/22/2019, 5:15 PM
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

Icaro Temponi

03/22/2019, 5:16 PM
hm, time to find another target to optimize then
7 Views