marcinmoskala
10/18/2021, 11:46 AMrunBlocking
is a coroutine builder, but its coroutine is bound to the thread, so its behavior is different from async
, launch
and produce
. Is there a name for coroutine builders except for runBlocking
?Erik
10/18/2021, 2:37 PMCoroutineScope
is inferred from the current thread in runBlocking
, while the other functions have the scope as the receiver. That's the only difference I see. However, I'd still call them all coroutine builders, or maybe coroutine factory functions. It is what they do: they build a coroutine for you.
async
, launch
and produce
do return some kind of handle to the coroutine: `Deferred<T>`/`Job`/`Channel<T>`, while runBlocking
returns T
(depending on the block). But that's just different signatures for different use cases.marcinmoskala
10/18/2021, 3:31 PMsequance
is a sequance builder, and List
is called a list builder.
https://kotlinlang.org/docs/coroutines-basics.html#your-first-coroutine
runBlocking
has actually very little in common with async
, launch
and produce
. In nearly all use cases it behaves differently. It needs to be at the top of hierarchy, its body is called in place (place one runBlocking
after another and they will will be called sequentially instead of concurrently). It behaves much more like coroutineScope
, and no wonder I hear people confusing them and asking about the differences between those two.Joffrey
10/18/2021, 3:55 PMrunBlocking
does behave more like coroutineScope
than other coroutine builders.
I would tend to call other coroutine builders "regular coroutine builders" as opposed to the special runBlocking
marcinmoskala
10/18/2021, 4:29 PMJoffrey
10/18/2021, 4:31 PMErik
10/18/2021, 5:04 PMrunBlocking
are very specific. But note that also with the other builders you can build blocking coroutines that run sequentially if called one after the other: just use a single-threaded blocking context.
Also, they all build coroutines. Coroutines don't specify concurrency or asynchronous behaviour. Their context does. In that sense the name 'coroutine builders' is good.Erik
10/18/2021, 5:10 PMrunBlocking
vs the others: implicit vs. explicit coroutine builders? Meh, maybe that's not a good name. I mean that the others are extensions on a coroutine scope, while runBlocking
implicitly uses a scope bound to the current thread.