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:38:27 +0200 |
| commit | dac66d0b5aac974af565b1924c472866b4956bf4 (patch) | |
| tree | e4e4ec1bb1ef7197fa5b697a499dd98d25a22a57 | |
| parent | c5f4b20bccd2443dd0fb502837d3ba58922e41d4 (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();
}
|
