You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by pa...@apache.org on 2015/11/18 18:41:05 UTC

[2/2] incubator-groovy git commit: The usage of the PROP_NAMES HashMap was not thread safe because the access was synchronized only for "put" operations and not for "get" operations: since HashMap is not thread safe all access to get/put operations must

The usage of the PROP_NAMES HashMap was not thread safe because the access was synchronized only for "put" operations and not for "get" operations: since HashMap is not thread safe all access to get/put operations must be checked. Reimplemented in a thread safe way by leveraging a ConcurrentHashMap: in this way the synchronized block is no more required. (closes #8)


Project: http://git-wip-us.apache.org/repos/asf/incubator-groovy/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-groovy/commit/e2f3627c
Tree: http://git-wip-us.apache.org/repos/asf/incubator-groovy/tree/e2f3627c
Diff: http://git-wip-us.apache.org/repos/asf/incubator-groovy/diff/e2f3627c

Branch: refs/heads/GROOVY_2_4_X
Commit: e2f3627c590ccfd8d60d3d7190e78abce0caa524
Parents: a865287
Author: Jacopo Cappellato <ja...@gmail.com>
Authored: Fri May 1 15:13:36 2015 +0200
Committer: pascalschumacher <pa...@gmx.net>
Committed: Wed Nov 18 18:40:52 2015 +0100

----------------------------------------------------------------------
 src/main/groovy/lang/MetaClassImpl.java | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-groovy/blob/e2f3627c/src/main/groovy/lang/MetaClassImpl.java
----------------------------------------------------------------------
diff --git a/src/main/groovy/lang/MetaClassImpl.java b/src/main/groovy/lang/MetaClassImpl.java
index 380ac07..e4bd34d 100644
--- a/src/main/groovy/lang/MetaClassImpl.java
+++ b/src/main/groovy/lang/MetaClassImpl.java
@@ -89,6 +89,8 @@ import java.security.AccessController;
 import java.security.PrivilegedActionException;
 import java.security.PrivilegedExceptionAction;
 import java.util.*;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentMap;
 
 /**
  * Allows methods to be dynamically added to existing classes at runtime
@@ -2421,20 +2423,18 @@ public class MetaClassImpl implements MetaClass, MutableMetaClass {
         }
     }
 
-    private static final Map<String, String> PROP_NAMES = new HashMap<String, String>(1024);
+    private static final ConcurrentMap<String, String> PROP_NAMES = new ConcurrentHashMap<String, String>(1024);
 
     private String getPropName(String methodName) {
         String name = PROP_NAMES.get(methodName);
-        if (name != null)
-            return name;
-
-        synchronized (PROP_NAMES) {
+        if (name == null) {
             // assume "is" or "[gs]et"
             String stripped = methodName.startsWith("is") ? methodName.substring(2) : methodName.substring(3);
             String propName = java.beans.Introspector.decapitalize(stripped);
-            PROP_NAMES.put(methodName, propName);
-            return propName;
+            PROP_NAMES.putIfAbsent(methodName, propName);
+            name = PROP_NAMES.get(methodName);
         }
+        return name;
     }
 
     private MetaProperty makeReplacementMetaProperty(MetaProperty mp, String propName, boolean isGetter, MetaMethod propertyMethod) {