ursus
07/13/2022, 12:31 AMdefault or io dispatcher? Or rather, when would I want to use io over default? I get that its in the name, but what exactly is io work?
Networking? Retrofit has coroutines support, so the thread wont be blocked there.
Database? Room has it as well
Only thing that comes to mind is file writing/reading?
Or would it be bad to just use the io? I mean it contracts eventually, right?
Can I see how many actual threads are in the io at some point?ephemient
07/13/2022, 12:40 AMDispatchers.Default is expected to be used for computation - e.g., things which are CPU-bound, so by default uses a thread pool bounded by the number of CPU cores detectedephemient
07/13/2022, 12:42 AMursus
07/13/2022, 12:42 AMursus
07/13/2022, 12:43 AMephemient
07/13/2022, 12:45 AMursus
07/13/2022, 12:45 AMephemient
07/13/2022, 12:45 AMursus
07/13/2022, 12:46 AMursus
07/13/2022, 12:46 AMsuspendCancelableCoroutine basicallyursus
07/13/2022, 12:47 AMephemient
07/13/2022, 12:47 AMephemient
07/13/2022, 12:48 AMursus
07/13/2022, 12:49 AMephemient
07/13/2022, 12:49 AMephemient
07/13/2022, 12:50 AMursus
07/13/2022, 12:50 AMursus
07/13/2022, 12:51 AMSyncer {
private val scope = CoroutineScope(<http://Dispatchers.IO|Dispatchers.IO>)
fun sync() {
scope.launch {
val whatever = 1 + 1
val apiData = apiClient.fetchData()
val data = apiData.toData()
database.save(data)
}
}
}ursus
07/13/2022, 12:52 AMephemient
07/13/2022, 12:52 AMursus
07/13/2022, 12:52 AMsync in a forloop, would I spin up 100 threads to do the 1+1 computation?ephemient
07/13/2022, 12:53 AMDispatchers.Main can run 100 parallel coroutines. it doess not require 100 threadsephemient
07/13/2022, 12:53 AMursus
07/13/2022, 12:53 AMephemient
07/13/2022, 12:54 AMursus
07/13/2022, 12:54 AMephemient
07/13/2022, 12:54 AMephemient
07/13/2022, 12:55 AMursus
07/13/2022, 12:56 AMursus
07/13/2022, 12:56 AMursus
07/13/2022, 12:57 AMfor syncer in hunderSyncers {
syncer.sync()
}ephemient
07/13/2022, 12:58 AMursus
07/13/2022, 12:58 AMfor syncer in hunderSyncers {
syncer.sync()
}
if the work is initiated like this, then you’d say for some short moment I do spin up 64 threads? Or whatever the number is, its 10s right? Way more that default would?ephemient
07/13/2022, 1:00 AMephemient
07/13/2022, 1:01 AMDefault depends on the systemephemient
07/13/2022, 1:01 AMIO and Default actually share the same underlying thread poolephemient
07/13/2022, 1:02 AMursus
07/13/2022, 1:02 AMioursus
07/13/2022, 1:03 AMephemient
07/13/2022, 1:03 AMIO.limitedParallelism can also create new views that can go beyond `IO`'s original boundsursus
07/13/2022, 1:04 AMDefaultDispatcher-worker-Xursus
07/13/2022, 1:06 AMdefault or io?ephemient
07/13/2022, 1:07 AMursus
07/13/2022, 1:07 AMephemient
07/13/2022, 1:08 AMursus
07/13/2022, 1:09 AMNick Allen
07/13/2022, 1:43 AMephemient
07/13/2022, 1:44 AMNick Allen
07/13/2022, 2:23 AMNick Allen
07/13/2022, 2:26 AMuli
07/13/2022, 5:10 AMephemient
07/13/2022, 5:17 AMephemient
07/13/2022, 5:19 AMsuspend fun sync() then you can launch(Dispatchers.Unconfined) to avoid switching threads until necessaryJoffrey
07/13/2022, 8:31 AMlimitedParallelism() to limit the thread spike. But honestly I think Default without limitedParallelism would be simple enough and allow to spawn all those coroutines and run them up to the first suspension point before they switch to the other thread pools.ursus
07/13/2022, 11:20 AMJoffrey
07/13/2022, 11:22 AMDefault dispatcher (and could cause starvation)uli
07/13/2022, 11:23 AM