7145454: JVM wide monitor lock in Currency.getInstance(String)
authornaoto
Wed, 21 Mar 2012 10:10:38 -0700
changeset 12202 2a63f5718d33
parent 12201 d77ed23f4992
child 12203 84a4382470c4
child 12291 1be435e23542
7145454: JVM wide monitor lock in Currency.getInstance(String) Reviewed-by: okutsu
jdk/src/share/classes/java/util/Currency.java
--- a/jdk/src/share/classes/java/util/Currency.java	Tue Mar 20 15:06:13 2012 -0700
+++ b/jdk/src/share/classes/java/util/Currency.java	Wed Mar 21 10:10:38 2012 -0700
@@ -34,6 +34,8 @@
 import java.io.Serializable;
 import java.security.AccessController;
 import java.security.PrivilegedAction;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentMap;
 import java.util.logging.Level;
 import java.util.regex.Pattern;
 import java.util.regex.Matcher;
@@ -99,7 +101,7 @@
 
     // class data: instance map
 
-    private static HashMap<String, Currency> instances = new HashMap<String, Currency>(7);
+    private static ConcurrentMap<String, Currency> instances = new ConcurrentHashMap<>(7);
     private static HashSet<Currency> available;
 
 
@@ -284,7 +286,6 @@
 
     private static Currency getInstance(String currencyCode, int defaultFractionDigits,
         int numericCode) {
-        synchronized (instances) {
             // Try to look up the currency code in the instances table.
             // This does the null pointer check as a side effect.
             // Also, if there already is an entry, the currencyCode must be valid.
@@ -322,10 +323,9 @@
                 }
             }
 
-            instance = new Currency(currencyCode, defaultFractionDigits, numericCode);
-            instances.put(currencyCode, instance);
-            return instance;
-        }
+        instance = instances.putIfAbsent(currencyCode,
+                   new Currency(currencyCode, defaultFractionDigits, numericCode));
+        return (instance != null ? instance : instances.get(currencyCode));
     }
 
     /**