https://kotlinlang.org logo
#android
Title
# android
b

Brendan Weinstein

12/03/2019, 7:38 PM
Dispatchers.IO defaults to being backed by an executor that can spawn up to 64 threads. Is it possible to architect an app with suspending non-blocking IO calls and we'd just use one thread for the IO dispatcher? Is it a reasonable expectation that migrating to coroutines+flow from rx will reduce the total number of threads an android application uses? This is the high-level question I am trying to answer.
b

bezrukov

12/03/2019, 7:51 PM
Most likely no, because under the hood almost any low-level io operation is blocking (e.g. reading from file, establishing a http connection etc).
z

Zach Klippenstein (he/him) [MOD]

12/03/2019, 8:22 PM
> Is it possible to architect an app with suspending non-blocking IO calls and we’d just use one thread for the IO dispatcher? Definitely, just create a single-threaded dispatcher and use that for IO instead of
<http://Dispatchers.IO|Dispatchers.IO>
But that means that if you try doing more than one IO operation at once, your app is just going to sit around doing nothing and waiting for the first operation to complete, which is not very efficient.
b

Brendan Weinstein

12/04/2019, 6:34 AM
@bezrukov We should be able to do io non-blocking with nio, no? Okhttp and okio do not appear to make use of nio's non-blocking apis, so I see what you are saying @Zach Klippenstein (he/him) [MOD] I am looking at Http1ExchangeCodex for reference. I did some digging and it looks like ktor's http client may offer what I have in mind.
image.png
CIOEngine creates a dispatcher backed by 4 threads (HttpClientEngineConfig.threadsCount "network threads count advice") It passes that dispatcher to ActorSelectorManager which launches a coroutine scoped to the dispatcher. And that coroutine just loops checking for new io work from the selector. I wonder if the 4 threads is server-side advice and we can just set threadsCount to 1 on android clients
b

bezrukov

12/04/2019, 8:35 AM
@Brendan Weinstein yes, using nio definitely makes sense
4 Views