From dee2a2b3e59144f89fc414c67c6837ab0657b998 Mon Sep 17 00:00:00 2001 From: maxli Date: Mon, 22 Jul 2024 22:03:02 +0800 Subject: [PATCH] fix(android): lru cache is thread safe --- .../com/openhippy/pool/ImageDataPool.java | 33 +++++++------------ 1 file changed, 12 insertions(+), 21 deletions(-) diff --git a/modules/android/pool/src/main/java/com/openhippy/pool/ImageDataPool.java b/modules/android/pool/src/main/java/com/openhippy/pool/ImageDataPool.java index 566edb4e12c..af5258655b5 100644 --- a/modules/android/pool/src/main/java/com/openhippy/pool/ImageDataPool.java +++ b/modules/android/pool/src/main/java/com/openhippy/pool/ImageDataPool.java @@ -23,7 +23,6 @@ public class ImageDataPool extends BasePool { private static final int DEFAULT_IMAGE_POOL_SIZE = 16; - private final Object mLock = new Object(); private LruCache mPools; public ImageDataPool() { @@ -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 @@ -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) {