changeset 8078:43db70cf8b20

7145454: JVM wide monitor lock in Currency.getInstance(String) Reviewed-by: okutsu
author naoto
date Wed, 21 Mar 2012 10:10:38 -0700
parents 715d90e998c9
children ffd8b8da0696
files src/share/classes/java/util/Currency.java
diffstat 1 files changed, 6 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/java/util/Currency.java	Wed Oct 08 14:25:04 2014 +0100
+++ b/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;
 
 
@@ -286,7 +288,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.
@@ -324,10 +325,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));
     }
 
     /**