Jimmy Alvarez
08/12/2021, 3:12 AMcoroutineScope.launch {
// some work
}
coroutineScope.launch {
// some other work
}
In my understanding, this should not block the thread given i’m launching both coroutines concurrently. but what is happening is that first coroutine blocks the thread so the second coroutine never gets executed.
The only way I could make it run concurrently was to wrap // Some work
into a suspend
function, then I got the concurrent execution
Could you explain a little why this only work by using the suspend function inside coroutine?Antanas A.
08/12/2021, 7:05 AMwithContext(<http://Dispatchers.IO|Dispatchers.IO>) { ... // some work }
in production env it's better to have some dedicated dispatcher with fixed thread pool for that kind of work, e.g.:
val blockingTaskDispatcher = Executors.newFixedThreadPool(32).asCoroutineDispatcher()
when use:
coroutineScope.launch {
withContext(blockingTaskDispatcher) {
// work here
}
}
and use it instead of Dispatchers.IO, because its thread pool is elastic and can span unlimited number of threads until memory overflow 🙂Jimmy Alvarez
08/12/2021, 7:13 PMnon-suspensable
function?Jimmy Alvarez
08/12/2021, 7:14 PM