https://kotlinlang.org logo
Title
d

David W

12/03/2021, 11:14 PM
Am I right in thinking that this, which I totally didn't write, will permanently tie up one of the few Default threads, and that four of these running on a 4-core machine would prevent any coroutines from running on Default?
val someStateFlow: StateFlow<Unit> = ...
GlobalScope.launch(Dispatchers.Default) { someStateFlow.collect { ... } }
"Don't use GlobalScope" yes I know
j

Joffrey

12/03/2021, 11:18 PM
As long as there is no long blocking operation done on the threads, it's not a problem per se. Collecting a stateflow never ends, indeed. But every iteration in the
collect
call has a suspension point, so the thread is regularly yielded to other coroutines (they can interlace freely)
You could technically have concurrency even with 1 thread this way
d

David W

12/03/2021, 11:21 PM
Oh, that makes perfect sense. Thanks (again)
👍 1
j

Joffrey

12/03/2021, 11:21 PM
But yeah, don't use
GlobalScope
😄
d

David W

12/03/2021, 11:22 PM
hahaha I'm trying