Fixed #1991 ConcurrentModificationException on clearing cache thread

This commit is contained in:
Jindra Petřík
2023-03-25 19:14:29 +01:00
parent 49013ed8d3
commit 8aa3c1830a
2 changed files with 42 additions and 32 deletions

View File

@@ -22,7 +22,6 @@ import java.io.File;
import java.io.IOException;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.ConcurrentModificationException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
@@ -43,6 +42,8 @@ public class Cache<K, V> implements Freed {
private Map<K, V> cache;
private Map<K, Long> lastAccessed;
private static final Object instancesLock = new Object();
private static final List<WeakReference<Cache>> instances = new ArrayList<>();
public static final int STORAGE_FILES = 1;
@@ -54,27 +55,28 @@ public class Cache<K, V> implements Freed {
private final boolean memoryOnly;
private final String name;
private final boolean temporary;
private static final long CLEAN_INTERVAL = 5 * 1000; //5 seconds
private static Thread oldCleaner = null;
private static Thread oldCleaner = null;
static {
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
for (WeakReference<Cache> cw : instances) {
Cache c = cw.get();
if (c != null) {
c.clear();
c.free();
synchronized (instancesLock) {
for (WeakReference<Cache> cw : instances) {
Cache c = cw.get();
if (c != null) {
c.clear();
c.free();
}
}
}
}
});
}
@@ -83,37 +85,41 @@ public class Cache<K, V> implements Freed {
oldCleaner = new Thread("Old cache cleaner") {
@Override
public void run() {
while(!Thread.interrupted()) {
while (!Thread.interrupted()) {
try {
Thread.sleep(CLEAN_INTERVAL);
} catch (InterruptedException ex) {
return;
}
try {
clearAllOld();
clearAllOld();
} catch (Exception cme) {
Logger.getLogger(Cache.class.getSimpleName()).log(Level.SEVERE, "Error during clearing cache thread", cme);
}
}
}
}
};
oldCleaner.setDaemon(true);
oldCleaner.setPriority(Thread.MIN_PRIORITY);
oldCleaner.start();
}
Cache<K, V> instance = new Cache<>(weak, memoryOnly, name, temporary);
instances.add(new WeakReference<>(instance));
synchronized (instancesLock) {
instances.add(new WeakReference<>(instance));
}
return instance;
}
private static int storageType = STORAGE_FILES;
public static void clearAll() {
for (WeakReference<Cache> cw : instances) {
Cache c = cw.get();
if (c != null) {
c.clear();
c.initCache();
synchronized (instancesLock) {
for (WeakReference<Cache> cw : instances) {
Cache c = cw.get();
if (c != null) {
c.clear();
c.initCache();
}
}
}
}
@@ -174,7 +180,7 @@ public class Cache<K, V> implements Freed {
initCache();
}
public synchronized boolean contains(K key) {
public synchronized boolean contains(K key) {
boolean ret = cache.containsKey(key);
if (ret) {
lastAccessed.put(key, System.currentTimeMillis());
@@ -208,7 +214,7 @@ public class Cache<K, V> implements Freed {
@Override
public boolean isFreeing() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
@@ -223,7 +229,7 @@ public class Cache<K, V> implements Freed {
ret.addAll(cache.keySet());
return ret;
}
private synchronized int clearOld() {
long currentTime = System.currentTimeMillis();
Set<K> keys = new HashSet<>(lastAccessed.keySet());
@@ -232,26 +238,28 @@ public class Cache<K, V> implements Freed {
return 0;
}
int num = 0;
for(K key:keys) {
for (K key : keys) {
long time = lastAccessed.get(key);
if (time < currentTime - temporaryThreshold) {
remove(key);
num++;
}
}
}
return num;
}
private static void clearAllOld() {
int num = 0;
for (WeakReference<Cache> cw : instances) {
Cache c = cw.get();
if (c != null) {
if (c.temporary) {
num += c.clearOld();
synchronized (instancesLock) {
for (WeakReference<Cache> cw : instances) {
Cache c = cw.get();
if (c != null) {
if (c.temporary) {
num += c.clearOld();
}
}
}
}
}
if (num > 0) {
System.gc();
}