How to increase Kotlin coroutines Dispatchers.IO size on Android?
You can create your own dispatcher with any number of threads, like so
val dispatcher = Executors.newFixedThreadPool(128).asCoroutineDispatcher()
With kotlinx-coroutines-1.6.0 you can limit the threads that are used for Dispatchers by using "limitedParallelism" method.
Example:
// 100 threads for MySQL connection
val myMysqlDbDispatcher = Dispatchers.IO.limitedParallelism(100)
// 60 threads for MongoDB connection
val myMongoDbDispatcher = Dispatchers.IO.limitedParallelism(60)
Release note: https://blog.jetbrains.com/kotlin/2021/12/introducing-kotlinx-coroutines-1-6-0/#dispatcher-views-api
IO_PARALLELISM_PROPERTY_NAME
does not refer to an Android system property, but to a Java system property. Just add this code early in your app, e.g. first in your Application.onCreate()
, to change it to 1000
:
import static kotlinx.coroutines.DispatchersKt.IO_PARALLELISM_PROPERTY_NAME;
System.setProperty(IO_PARALLELISM_PROPERTY_NAME, Integer.toString(1000));
This does not have to be done on a per-device basis with root or anything like that. It will work everywhere, since it is regular app code using regular app APIs.
As long as you do this before you use Dispatchers.IO
for the first time, your property change will be applied.