Leo N
08/19/2024, 7:55 AMoverride fun getCmsText(
languageId: Int,
cmsId: Int,
origin: String,
): CompletableFuture<String> {
return CoroutineScope(Dispatchers.IO).future {
service.getCmsText(languageId, cmsId, origin) // service.getCmsItem is a suspendable function
}
}
Scala devs are concerned that Dispatchers.IO will initialize a separate thread pool just for this library and would rather reuse the threads they already have. If we use Dispatcher.Default would that prevent creating of yet another pool for Coroutines?Sam
08/19/2024, 7:59 AMexecutor.asCoroutineDispatcher()
.Joffrey
08/19/2024, 7:59 AMSam
08/19/2024, 7:59 AMCoroutineScope(<http://Dispatchers.IO|Dispatchers.IO>)
isn't a great solution. If you really want a job with no parent, GlobalScope.future(dispatcher)
makes the intent clearer.Leo N
08/19/2024, 9:34 AMuli
08/20/2024, 7:57 AMgetCmsText
?Leo N
08/20/2024, 8:30 AMuli
08/20/2024, 9:53 AM• If your function is a suspending function, there’s no need to use Dispatchers.IO. It’s specifically designed for non-suspending functions that block their thread.What does
getCmsText
suspend on? Do you need a dispatcher at all?
Let’s assume as an example, you are using okhttp. Then blocking operations will be scheduled to okhttp’s own thread pool.
If at all developed by the conventions, getCmsText
as a suspend function should not block any of your threads. It would either use non-blocking IO or make sure blocking operations are scheduled to some background threads.uli
08/20/2024, 9:57 AMgetCmsText
obays to the conventions, You are fine with Dispatchers.Unconfined
Then the only question is, does getCmsText
itself introduce a thread pool your Scala friends would object to.