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

vineethraj49

11/06/2019, 8:42 AM
is this (spawning
async(Dispatchers.Default)
on
runBlocking
) ok, or will this blow up in my face?
o

octylFractal

11/06/2019, 8:44 AM
no issue, but you may consider off-loading your blocking calls to
<http://Dispatchers.IO|Dispatchers.IO>
for better coroutine integration
v

vineethraj49

11/06/2019, 8:45 AM
runMany
has some blocking
loadUrl
calls, defined as such:
Copy code
suspend fun loadUrl(url: String) = withContext(<http://Dispatchers.IO|Dispatchers.IO>) {
...}
Copy code
suspend fun runMany(): List<String> = coroutineScope {
   ...
   val one = async { loadUrl() } 
   val two = async { loadUrl() }

   return listOf(one.await(), two.await())
}
so
runMany
is in
Dispatchers.Default
, the suspended
loadUrl
inside is on
<http://Dispatchers.IO|Dispatchers.IO>
1. is there any way to get dispatcher information as well? 2. If I have too many calls on Dispatchers.IO, will I starve the
DefaultDispatcher
(which “shares threads with a [Default][Dispatchers.Default]“) or will the calls on Dispatchers.IO suspend and give back the threads to Dispatchers.Default?
(context around 1, I have this definition of log
fun log(msg: String) = println("[${Thread.currentThread().name}] $msg")
with
-Dkotlinx.coroutines.debug
enabled
o

octylFractal

11/06/2019, 8:53 AM
1. not sure what you mean by information, you can extract the current ContinuationInterceptor and try to cast it:
coroutineContext[ContinuationInterceptor]
2. you won't starve the default dispatcher afaik, not sure how the specifics work out
my understanding of (2) is that the default dispatcher never uses more than #-processors of IO dispatchers thread pool, not that it uses a strict subset of them
b

bdawg.io

11/06/2019, 9:14 AM
They have independent pool limits, but share threads. For example, if a Default thread is needed it will create another one, but it returns it to the shared pool when it's not in use
5 Views