svn commit: r883222 - /ofbiz/trunk/framework/base/src/org/ofbiz/base/util/cache/UtilCache.java

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

svn commit: r883222 - /ofbiz/trunk/framework/base/src/org/ofbiz/base/util/cache/UtilCache.java

doogie-3
Author: doogie
Date: Mon Nov 23 03:00:49 2009
New Revision: 883222

URL: http://svn.apache.org/viewvc?rev=883222&view=rev
Log:
Fix defaultIndices manipulation, by switching to ConcurrentHashMap; this
has a good side-effect of fixing the total lack of synchronization on
the previous version.

Modified:
    ofbiz/trunk/framework/base/src/org/ofbiz/base/util/cache/UtilCache.java

Modified: ofbiz/trunk/framework/base/src/org/ofbiz/base/util/cache/UtilCache.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/src/org/ofbiz/base/util/cache/UtilCache.java?rev=883222&r1=883221&r2=883222&view=diff
==============================================================================
--- ofbiz/trunk/framework/base/src/org/ofbiz/base/util/cache/UtilCache.java (original)
+++ ofbiz/trunk/framework/base/src/org/ofbiz/base/util/cache/UtilCache.java Mon Nov 23 03:00:49 2009
@@ -27,9 +27,9 @@
 import java.util.ResourceBundle;
 import java.util.Set;
 import java.util.WeakHashMap;
+import java.util.concurrent.ConcurrentHashMap;
 
 import javolution.util.FastList;
-import javolution.util.FastMap;
 import javolution.util.FastSet;
 
 import org.ofbiz.base.util.Debug;
@@ -55,7 +55,7 @@
     public static final Map<String, UtilCache<?, ?>> utilCacheTable = new WeakHashMap<String, UtilCache<?, ?>>();
 
     /** An index number appended to utilCacheTable names when there are conflicts. */
-    private final static Map<String, Integer> defaultIndices = FastMap.newInstance();
+    private final static ConcurrentHashMap<String, Integer> defaultIndices = new ConcurrentHashMap<String, Integer>();
 
     /** The name of the UtilCache instance, is also the key for the instance in utilCacheTable. */
     private final String name;
@@ -181,15 +181,21 @@
     }
 
     private static String getNextDefaultIndex(String cacheName) {
-        Integer curInd = UtilCache.defaultIndices.get(cacheName);
-
-        if (curInd == null) {
-            UtilCache.defaultIndices.put(cacheName, 1);
-            return "";
-        } else {
-            UtilCache.defaultIndices.put(cacheName, curInd + 1);
-            return Integer.toString(curInd + 1);
-        }
+        Integer curInd;
+        do {
+            curInd = defaultIndices.get(cacheName);
+            if (curInd == null) {
+                if (defaultIndices.putIfAbsent(cacheName, 1) == null) {
+                    // no one else was able to store a value before us
+                    break;
+                }
+            } else if (defaultIndices.replace(cacheName, curInd, curInd + 1)) {
+                // replaced the current value, so we know that this iteration
+                // has control over curInd
+                break;
+            }
+        } while (true);
+        return curInd == null ? "" : Integer.toString(curInd + 1);
     }
 
     public static String getPropertyParam(ResourceBundle res, String[] propNames, String parameter) {