优化
This commit is contained in:
@@ -38,6 +38,7 @@ import kotlinx.coroutines.flow.onEach
|
|||||||
import kotlinx.coroutines.isActive
|
import kotlinx.coroutines.isActive
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
import java.util.LinkedList
|
import java.util.LinkedList
|
||||||
|
import java.util.concurrent.ConcurrentHashMap
|
||||||
import java.util.concurrent.Executors
|
import java.util.concurrent.Executors
|
||||||
import kotlin.math.min
|
import kotlin.math.min
|
||||||
|
|
||||||
@@ -46,7 +47,7 @@ class MainViewModel(application: Application) : BaseViewModel(application) {
|
|||||||
private var poolSize = min(threadCount, AppConst.MAX_THREAD)
|
private var poolSize = min(threadCount, AppConst.MAX_THREAD)
|
||||||
private var upTocPool = Executors.newFixedThreadPool(poolSize).asCoroutineDispatcher()
|
private var upTocPool = Executors.newFixedThreadPool(poolSize).asCoroutineDispatcher()
|
||||||
private val waitUpTocBooks = LinkedList<String>()
|
private val waitUpTocBooks = LinkedList<String>()
|
||||||
private val onUpTocBooks = hashSetOf<String>()
|
private val onUpTocBooks = ConcurrentHashMap.newKeySet<String>()
|
||||||
val onUpBooksLiveData = MutableLiveData<Int>()
|
val onUpBooksLiveData = MutableLiveData<Int>()
|
||||||
private var upTocJob: Job? = null
|
private var upTocJob: Job? = null
|
||||||
private var cacheBookJob: Job? = null
|
private var cacheBookJob: Job? = null
|
||||||
@@ -110,10 +111,9 @@ class MainViewModel(application: Application) : BaseViewModel(application) {
|
|||||||
while (true) {
|
while (true) {
|
||||||
emit(waitUpTocBooks.poll() ?: break)
|
emit(waitUpTocBooks.poll() ?: break)
|
||||||
}
|
}
|
||||||
}.onEach {
|
}.onEachParallel(threadCount) {
|
||||||
onUpTocBooks.add(it)
|
onUpTocBooks.add(it)
|
||||||
postEvent(EventBus.UP_BOOKSHELF, it)
|
postEvent(EventBus.UP_BOOKSHELF, it)
|
||||||
}.onEachParallel(threadCount) {
|
|
||||||
updateToc(it)
|
updateToc(it)
|
||||||
}.onEach {
|
}.onEach {
|
||||||
onUpTocBooks.remove(it)
|
onUpTocBooks.remove(it)
|
||||||
|
|||||||
Reference in New Issue
Block a user