防止同时执行两个挂起函数的一部分
Prevent concurrent execution of part of two suspend functions
我有两个在 ViewModel 中并行启动的挂起方法:
init {
viewModelScope.launch(Dispatchers.Default) {
launch { loadTotalCirculation() }
launch { loadMarketPrice() }
}
}
private suspend fun loadTotalCirculation() {
val totalCirculation = bitcoinChartsRepository.getTotalCirculation(5, TimeUnit.HOURS)
_viewState.value = _viewState.value.copy(totalCirculation = chartViewEntityMapper(totalCirculation))
}
private suspend fun loadMarketPrice() {
val marketPrice = bitcoinChartsRepository.getMarketPrice(27, TimeUnit.DAYS)
_viewState.value = _viewState.value.copy(marketPrice = chartViewEntityMapper(marketPrice))
}
但是,我想在我的两个方法中阻止 _viewState.value = _viewState.value.copy...
部分的并发执行。实现此目标的最佳方法是什么?
有几种同步并行协程的可能性。可能最简单的是创建一个单线程上下文,类似于主线程或使用 Mutex。请注意,此互斥量是专门为协程设计的,它不是 Java stdlib 中的东西。
单线程上下文:
val context = Executors.newSingleThreadExecutor().asCoroutineDispatcher()
private suspend fun loadTotalCirculation() {
val totalCirculation = bitcoinChartsRepository.getTotalCirculation(5, TimeUnit.HOURS)
withContext (context) {
_viewState.value = _viewState.value.copy(totalCirculation = chartViewEntityMapper(totalCirculation))
}
}
private suspend fun loadMarketPrice() {
val marketPrice = bitcoinChartsRepository.getMarketPrice(27, TimeUnit.DAYS)
withContext (context) {
_viewState.value = _viewState.value.copy(marketPrice = chartViewEntityMapper(marketPrice))
}
}
或者,您可以通过以下方式重用主线程,而不是创建自己的线程:withContext(Dispatchers.Main)
。
互斥:
val mutex = Mutex()
private suspend fun loadTotalCirculation() {
val totalCirculation = bitcoinChartsRepository.getTotalCirculation(5, TimeUnit.HOURS)
mutex.withLock {
_viewState.value = _viewState.value.copy(totalCirculation = chartViewEntityMapper(totalCirculation))
}
}
private suspend fun loadMarketPrice() {
val marketPrice = bitcoinChartsRepository.getMarketPrice(27, TimeUnit.DAYS)
mutex.withLock {
_viewState.value = _viewState.value.copy(marketPrice = chartViewEntityMapper(marketPrice))
}
}
可能首选使用主线程或互斥锁,因为如果我们创建自己的线程,我们需要确保在不再需要它时正确地stop/close它。
您可以从官方文档中阅读本文的更多内容:https://kotlinlang.org/docs/shared-mutable-state-and-concurrency.html
我有两个在 ViewModel 中并行启动的挂起方法:
init {
viewModelScope.launch(Dispatchers.Default) {
launch { loadTotalCirculation() }
launch { loadMarketPrice() }
}
}
private suspend fun loadTotalCirculation() {
val totalCirculation = bitcoinChartsRepository.getTotalCirculation(5, TimeUnit.HOURS)
_viewState.value = _viewState.value.copy(totalCirculation = chartViewEntityMapper(totalCirculation))
}
private suspend fun loadMarketPrice() {
val marketPrice = bitcoinChartsRepository.getMarketPrice(27, TimeUnit.DAYS)
_viewState.value = _viewState.value.copy(marketPrice = chartViewEntityMapper(marketPrice))
}
但是,我想在我的两个方法中阻止 _viewState.value = _viewState.value.copy...
部分的并发执行。实现此目标的最佳方法是什么?
有几种同步并行协程的可能性。可能最简单的是创建一个单线程上下文,类似于主线程或使用 Mutex。请注意,此互斥量是专门为协程设计的,它不是 Java stdlib 中的东西。
单线程上下文:
val context = Executors.newSingleThreadExecutor().asCoroutineDispatcher()
private suspend fun loadTotalCirculation() {
val totalCirculation = bitcoinChartsRepository.getTotalCirculation(5, TimeUnit.HOURS)
withContext (context) {
_viewState.value = _viewState.value.copy(totalCirculation = chartViewEntityMapper(totalCirculation))
}
}
private suspend fun loadMarketPrice() {
val marketPrice = bitcoinChartsRepository.getMarketPrice(27, TimeUnit.DAYS)
withContext (context) {
_viewState.value = _viewState.value.copy(marketPrice = chartViewEntityMapper(marketPrice))
}
}
或者,您可以通过以下方式重用主线程,而不是创建自己的线程:withContext(Dispatchers.Main)
。
互斥:
val mutex = Mutex()
private suspend fun loadTotalCirculation() {
val totalCirculation = bitcoinChartsRepository.getTotalCirculation(5, TimeUnit.HOURS)
mutex.withLock {
_viewState.value = _viewState.value.copy(totalCirculation = chartViewEntityMapper(totalCirculation))
}
}
private suspend fun loadMarketPrice() {
val marketPrice = bitcoinChartsRepository.getMarketPrice(27, TimeUnit.DAYS)
mutex.withLock {
_viewState.value = _viewState.value.copy(marketPrice = chartViewEntityMapper(marketPrice))
}
}
可能首选使用主线程或互斥锁,因为如果我们创建自己的线程,我们需要确保在不再需要它时正确地stop/close它。
您可以从官方文档中阅读本文的更多内容:https://kotlinlang.org/docs/shared-mutable-state-and-concurrency.html