Skip to content

Commit

Permalink
fix(android): lru cache is thread safe
Browse files Browse the repository at this point in the history
  • Loading branch information
siguangli2018 authored and siguangli committed Jul 22, 2024
1 parent 2ff8a19 commit b5c2d1d
Showing 1 changed file with 12 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
public class ImageDataPool extends BasePool<ImageDataKey, ImageRecycleObject> {

private static final int DEFAULT_IMAGE_POOL_SIZE = 16;
private final Object mLock = new Object();
private LruCache<ImageDataKey, ImageRecycleObject> mPools;

public ImageDataPool() {
Expand Down Expand Up @@ -51,17 +50,15 @@ protected void entryRemoved(boolean evicted, @NonNull ImageDataKey key,
@Override
@Nullable
public ImageRecycleObject acquire(@NonNull ImageDataKey key) {
synchronized (mLock) {
ImageRecycleObject data = mPools.get(key);
if (data != null && data.isScraped()) {
// Bitmap may have been recycled, must be removed from the cache and not
// returned to the component.
mPools.remove(key);
data.evicted();
return null;
}
return data;
ImageRecycleObject data = mPools.get(key);
if (data != null && data.isScraped()) {
// Bitmap may have been recycled, must be removed from the cache and not
// returned to the component.
mPools.remove(key);
data.evicted();
return null;
}
return data;
}

@Override
Expand All @@ -74,24 +71,18 @@ public void release(@NonNull ImageRecycleObject data) {

@Override
public void release(@NonNull ImageDataKey key, @NonNull ImageRecycleObject data) {
synchronized (mLock) {
mPools.put(key, data);
data.cached();
}
mPools.put(key, data);
data.cached();
}

@Override
public void clear() {
synchronized (mLock) {
mPools.evictAll();
}
mPools.evictAll();
}

@Override
public void remove(@NonNull ImageDataKey key) {
synchronized (mLock) {
mPools.remove(key);
}
mPools.remove(key);
}

private void onEntryEvicted(@NonNull ImageRecycleObject data) {
Expand Down

0 comments on commit b5c2d1d

Please sign in to comment.