Skip to content

Commit

Permalink
修复多线程导致的ConcurrentModificationException
Browse files Browse the repository at this point in the history
  • Loading branch information
ltttttttttttt committed Apr 22, 2024
1 parent 1f00aa8 commit 7564c05
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 7 deletions.
26 changes: 26 additions & 0 deletions common/src/commonMain/kotlin/com/lt/common/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
package com.lt.common

import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.LazyListState
Expand Down Expand Up @@ -54,3 +58,25 @@ fun App(
}
}
}

val moreImages = mutableListOf<String>().apply {
repeat(9999) {
add("https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fbkimg.cdn.bcebos.com%2Fpic%2Ff9198618367adab44aed43580398a41c8701a18b4ed1&refer=http%3A%2F%2Fbkimg.cdn.bcebos.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1651992355&t=0eb27c606fc381fdaaaca828505ac114&a=$it&b=")
}
}

//更多图片测试
@Composable
fun MoreImage() {
LazyColumn(Modifier.fillMaxSize()) {
items(moreImages) {
Row(Modifier.height(50.dp)) {
repeat(10) { i ->
Image(
rememberImagePainter(it + i), "", Modifier.weight(1f).fillMaxHeight()
)
}
}
}
}
}
4 changes: 3 additions & 1 deletion desktop/src/jvmMain/kotlin/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.Window
import androidx.compose.ui.window.application
import com.lt.common.App
import com.lt.common.MoreImage
import com.lt.load_the_image.LoadTheImageManager
import com.lt.load_the_image.loader.DataToBeLoaded
import com.lt.load_the_image.rememberImagePainter
Expand All @@ -46,7 +47,8 @@ fun main() {
Window(onCloseRequest = ::exitApplication) {
MaterialTheme {
//TestLazyList()
UI()
//UI()
MoreImage()
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@

distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.2-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-7.5-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

package com.lt.load_the_image.cache

import java.util.Collections

/**
* creator: lt 2022/4/8 [email protected]
* effect : Memory cache configuration of network image, cache use LRU
Expand All @@ -27,13 +25,12 @@ open class ImageLruMemoryCache(
private val maxMemorySize: Long = getMemoryWithOnePercent()
) : ImageCache {
//image lru cache
private val cacheMap = Collections.synchronizedMap(
LinkedHashMap<String, ByteArray>(35, 1f, true)
)
private val cacheMap = LinkedHashMap<String, ByteArray>(35, 1f, true)

//image cache byte size sum
private var cacheSize: Long = 0

@Synchronized
override fun saveCache(url: String, t: ByteArray) {
if (t.size > maxMemorySize)
return
Expand All @@ -45,6 +42,7 @@ open class ImageLruMemoryCache(
}
}

@Synchronized
override fun getCache(url: String): ByteArray? {
return cacheMap[url]
}
Expand Down

0 comments on commit 7564c05

Please sign in to comment.