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

Ahmed Ibrahim

10/08/2020, 1:10 PM
I'm using Room Database along with their coroutines support for managing the DB in my app, and I'm doing lots of queries in transaction since they span multiple relationships. I noticed that they use a thread pool of 4 threads to execute queries there, however this causes some long loading times due to queries being queued until one of the 4 threads is free to continue executions. I thought about customizing the Query executor by providing
<http://Dispatchers.IO|Dispatchers.IO>.asExecutor()
, but I'm wondering if that would be an appropriate solution since as far as I learned
Dispatchers.Default
shares some threads from IO and that might cause another set of problems. So the question is, would that be a good solution to the problem from multi-threading perspective?
I also started seeing from the logs
Copy code
Long monitor contention with owner DefaultDispatcher-worker-6 (27719) at android.database.sqlite.SQLiteConnection android.database.sqlite.SQLiteConnectionPool.waitForConnection(java.lang.String, int, android.os.CancellationSignal)(SQLiteConnectionPool.java:-1) waiters=0 in void android.database.sqlite.SQLiteConnectionPool.closeAvailableNonPrimaryConnectionsAndLogExceptions() for 2.511s
However not sure if I should ignore them
Not sure TBH if this question should stay in #coroutines or moved to #android
g

gildor

10/08/2020, 2:56 PM
What kind set of problems could be caused by the fact that Default and IO share thread pool?
Why do you need a special thread pool for Room, isn't it already provide coroutine api and it uses IO under the hood
a

Ahmed Ibrahim

10/08/2020, 3:09 PM
What kind set of problems could be caused by the fact that Default and IO share thread pool?
I'm not entirely sure, but perhaps since Default executor is supposed to be limited by the number of CPU core, it might suffer from thread starvation issues, as the IO would consume from Default's pool. (I might've gotten that wrongly though)
Why do you need a special thread pool for Room, isn't it already provide coroutine api and it uses IO under the hood
It provides suspending calls, but it uses their own dispatchers as per this link, which by default is a newFixedThreadPool
b

bdawg.io

10/08/2020, 5:56 PM
They SHARE a pool but manage their limits independently. So default will have its thread limit available even if the maximum number IO threads are actively in use
👌 1
☝️ 1
g

gildor

10/09/2020, 12:34 AM
As Bradyn said, you also can configure IO and Default pools independently
66 Views