Possible atomicity violations when composing concurrent collections operations

David Holmes david.holmes at oracle.com
Mon Aug 6 15:50:18 PDT 2012


The ClassLoader case hadn't been addressed yet so ...

On 3/08/2012 4:52 AM, Yu Lin wrote:
> Also, there are some code in OpenJDK7u that avoid the above
> interleaving by adding synchronizations. For example in
> jdk/src/share/classes/java/lang/ClassLoader.java from line 932 to 937
>
> L932   synchronized (this) {
> L933       pcerts = package2certs.get(pname);
> L934       if (pcerts == null) {
> L935           package2certs.put(pname, (certs == null? nocerts:certs));
> L936       }
> L937   }
>
> However, if we use "putIfAbsent" at line 592, the synchronization at
> line 588 is no longer needed. Generally, using "putIfAbsent" has
> better performace than synchronizing a concurrent hashmap.

Note that if the above code is executed it means that we are not dealing 
with a parallel-capable loader, which means that package2certs is a 
Hashtable not a ConcurrentHashMap. The complete code section is actually:

         if (parallelLockMap == null) {
             synchronized (this) {
                 pcerts = package2certs.get(pname);
                 if (pcerts == null) {
                     package2certs.put(pname, (certs == null? 
nocerts:certs));
                 }
             }
         } else {
             pcerts = ((ConcurrentHashMap<String, 
Certificate[]>)package2certs).
                 putIfAbsent(pname, (certs == null? nocerts:certs));
         }

David
-----


More information about the jdk8-dev mailing list