Luis Munoz
06/09/2020, 9:17 PMval singleThread = newSingleThreadContext("SingleThread")
val flow = flow {
emit("a")
blocking call
delay(1000)
emit("b")
}.flowOn(singleThread)
launch(<http://Dispachers.IO|Dispachers.IO>) {
flow.collect {
println(it)
}
}
launch(<http://Dispachers.IO|Dispachers.IO>) {
flow.collect {
println(it)
}
}
octylFractal
06/09/2020, 9:18 PMLuis Munoz
06/09/2020, 9:20 PMoctylFractal
06/09/2020, 9:23 PMLuis Munoz
06/09/2020, 9:25 PMoctylFractal
06/09/2020, 9:26 PMLuis Munoz
06/09/2020, 9:27 PMoctylFractal
06/09/2020, 9:27 PMLuis Munoz
06/09/2020, 9:28 PMoctylFractal
06/09/2020, 9:29 PMdelay
you have in the single-thread section, that can allow the other flow to start processing, because the thread is available for executing workLuis Munoz
06/09/2020, 9:29 PM.flowOn(singleThread)
octylFractal
06/09/2020, 9:30 PMflowOn
only affects code prior to that call -- you can use e.g. HashMap safely, but you may not assume that the block runs to completion before another one startsMutex
to ensure that it can't execute simultaneously (it's basically a suspending lock)Rechee Jozil
06/10/2020, 6:38 AMLuis Munoz
06/10/2020, 3:14 PM