You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@wink.apache.org by ro...@apache.org on 2010/02/20 20:58:07 UTC

svn commit: r912204 - /incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/internal/utils/SoftConcurrentMap.java

Author: rott
Date: Sat Feb 20 19:58:07 2010
New Revision: 912204

URL: http://svn.apache.org/viewvc?rev=912204&view=rev
Log:
WINK-255: protect against ConcurrentModificationException when garbage collector thread tries to clean up SoftReferences in the WeakHashMap

Modified:
    incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/internal/utils/SoftConcurrentMap.java

Modified: incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/internal/utils/SoftConcurrentMap.java
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/internal/utils/SoftConcurrentMap.java?rev=912204&r1=912203&r2=912204&view=diff
==============================================================================
--- incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/internal/utils/SoftConcurrentMap.java (original)
+++ incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/internal/utils/SoftConcurrentMap.java Sat Feb 20 19:58:07 2010
@@ -19,6 +19,7 @@
 package org.apache.wink.common.internal.utils;
 
 import java.lang.ref.SoftReference;
+import java.util.ConcurrentModificationException;
 import java.util.Map;
 import java.util.WeakHashMap;
 
@@ -74,7 +75,26 @@
      * @return val - the current value.
      */
     public synchronized V put(K key, V val) {
-        WeakHashMap<K, SoftReference<V>> copyOfMap = new WeakHashMap<K, SoftReference<V>>(map);
+        /*
+         * under the WeakHashMap(Map) constructor, java.util.AbstractMap.putAll is called, which uses
+         * an iterator.  Iterators, as we all know, are not thread-safe; they are susceptible
+         * to ConcurrentModificationExceptions.  Note that this method is already 'synchronized'.
+         * However, that does not protect this.map from the silent garbage collector thread, which
+         * may remove something at any time due to the internal values being "SoftReferences".
+         * Instead of synchronizing on this.map, let's just catch ConcurrentModificationException
+         * ignore it, and retry in the while loop.
+         */
+        // non-null to avoid NPE when the timing of multi-threaded runs conspire against us
+        WeakHashMap<K, SoftReference<V>> copyOfMap = new WeakHashMap<K, SoftReference<V>>();
+        boolean complete = false;
+        while (!complete) {
+            try {
+                copyOfMap = new WeakHashMap<K, SoftReference<V>>(map);
+                complete = true;
+            } catch (ConcurrentModificationException e) {
+                // ignored
+            }
+        }
         copyOfMap.put(key, new SoftReference<V>(val));
         map = copyOfMap;
         return val;