协程块中具有简单变量,并且只有一个生产者协程会更改变量的状态,而另一个协程将其消耗,如果此变量与AtomicReference或Channel以某种方式同步,以便在阅读器协程中可见,或者阅读器协程会自动从中获取新数据记忆吗?
fun main() = runBlocking<Unit>(Dispatchers.Default) {
var state: State? = null
launch {
data0Flow.collect { newState ->
state = newState
}
}
launch {
data1Flow.collect { someData ->
// Will the state be visible immediately when first coroutine updated it ?
if (state != null) {
// do something
}
}
}
}