dephinera
01/09/2023, 11:16 AMdelay
fixes (kind of) the issue, but the result doesn’t start from “0”?
3. Why does changing the dispatcher of pubScope
to Default
makes the behavior similar to the one mentioned in 2?dephinera
01/09/2023, 11:16 AMfun main() {
val sharedFlow = MutableSharedFlow<Int>(extraBufferCapacity = 50, onBufferOverflow = BufferOverflow.SUSPEND)
val times = 100
val consumerJob = Job()
val consumeScope = CoroutineScope(Dispatchers.Default) + consumerJob
val pubJob = Job()
val pubScope = CoroutineScope(Dispatchers.Unconfined) + pubJob
consumeScope.launch(Dispatchers.Default) {
println("launched subscriber")
sharedFlow
.takeWhile { it < times }
.collect {
delay(100)
println("Collect: $it")
}
}
println("launch publishers")
repeat(times + 1) {
pubScope.launch {
// delay(10)
// println("launched: $it")
sharedFlow.emit(it)
}
}
runBlocking {
consumerJob.join()
pubJob.join()
}
}
bezrukov
01/09/2023, 12:15 PMconsumeScope.launch(Dispatchers.Default, start = CoroutineStart.UNDISPATCHED) {
dephinera
01/09/2023, 12:37 PMtakeWhile
? The program gets stuck at the end without exiting. It seems like the join()
calls never resumedephinera
01/09/2023, 12:37 PMconsumerJob
but the publisher
one as wellbezrukov
01/09/2023, 12:42 PMval pubJob = pubScope.launch(..) { ... }
val consumerJob = consumerScope.launch(..) { .. }
bezrukov
01/09/2023, 12:44 PMbezrukov
01/09/2023, 12:45 PMdephinera
01/09/2023, 1:01 PM