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

Cody Mikol

04/02/2021, 4:35 PM
Does running a series of coroutine operations in a blocking context force all inner functionality to run synchronously? say you had something like
Copy code
runBlocking {

  val (foo,bar) = doAsync({ foo() },{ bar() })
  
  val (baz,bax) = doAsync({ baz(foo) },{ bax(bar) })

  return listOf(foo,bar,baz,bax)

}
Assuming doAsync runs the inner lambdas concurrently, would runBlocking the outer scope force these to run synchronously ?
w

wasyl

04/02/2021, 4:45 PM
No, runBlocking doesn’t mean thing inside are blocking one another, it’s just a coroutine builder that only returns after the coroutines inside have finished
But you still need to have enough threads available if things run in parallel, like
withContext(<http://Dispatchers.IO|Dispatchers.IO>)
for example (unless the async stuff can run in concurrently on one thread)
v

vineethraj49

04/03/2021, 1:10 AM
async stuff can run in concurrently on one thread => foo, bar, baz, and bax are suspending, and have suspension points in them ?
t

TwoClocks

04/03/2021, 5:18 AM
Yes. I use async quite a bit, but for my use-case I only run 1 thread. So I effectively have a bunch of "green threads" that are doing cooperative multitasking. And yes, every time they hit something that suspends, they effective give up control and let something else run.
3 Views