diff options
| author | Andrew Branson <andrew.branson@cern.ch> | 2013-09-20 13:38:27 +0200 |
|---|---|---|
| committer | Andrew Branson <andrew.branson@cern.ch> | 2013-09-20 13:39:43 +0200 |
| commit | e5353d60af8e80444721384cb7cb7b2c3465b225 (patch) | |
| tree | 8cab2256828e4d2bb2a2fed6da65b68c2d331e3b | |
| parent | 2640d2bf94019764cfbcbd2a4adf0b165ac360d8 (diff) | |
Synchronize on the hardCache, as it occasionally causes exceptions.
| -rw-r--r-- | src/main/java/com/c2kernel/utils/SoftCache.java | 20 |
1 files changed, 13 insertions, 7 deletions
diff --git a/src/main/java/com/c2kernel/utils/SoftCache.java b/src/main/java/com/c2kernel/utils/SoftCache.java index 2fd79f1..efc1614 100644 --- a/src/main/java/com/c2kernel/utils/SoftCache.java +++ b/src/main/java/com/c2kernel/utils/SoftCache.java @@ -39,9 +39,11 @@ public class SoftCache<K, V> extends AbstractMap<K, V> { hash.remove(key);
else
if (minSize > 0) { // add to hard cache so it's not reaped for a while
- hardCache.addFirst(result);
- if (hardCache.size() > minSize) // trim last one off
- hardCache.removeLast();
+ synchronized(hardCache) {
+ hardCache.addFirst(result);
+ if (hardCache.size() > minSize) // trim last one off
+ hardCache.removeLast();
+ }
}
}
return result;
@@ -51,9 +53,11 @@ public class SoftCache<K, V> extends AbstractMap<K, V> { public V put(K key, V value) {
processQueue();
if (minSize > 0) {
- hardCache.addFirst(value);
- if (hardCache.size() > minSize)
- hardCache.removeLast();
+ synchronized(hardCache) {
+ hardCache.addFirst(value);
+ if (hardCache.size() > minSize)
+ hardCache.removeLast();
+ }
}
hash.put(key, new SoftValue<V>(key, value, queue));
return value;
@@ -68,7 +72,9 @@ public class SoftCache<K, V> extends AbstractMap<K, V> { @Override
public void clear() {
- hardCache.clear();
+ synchronized(hardCache) {
+ hardCache.clear();
+ }
while(queue.poll()!=null);
hash.clear();
}
|
