[concurrency-interest] 8145539: (coll) AbstractMap.keySet and .values should not be volatile

Volker.Borchert at atis-systems.com Volker.Borchert at atis-systems.com
Wed Apr 20 08:01:49 EDT 2016


Sorry if this is inappropiate here...

I'm just browsing 1.8.0_92 release notes and the following question arose.

http://hg.openjdk.java.net/jdk9/jdk9/jdk/rev/ce72c7641f38 states

+     * <p>It is also imperative that implementations read the field only once,
+     * as in:
+     *
+     * <pre> {@code
+     * public Set<K> keySet() {
+     *   Set<K> ks = keySet;  // single racy read
+     *   if (ks == null) {
+     *     ks = new KeySet();
+     *     keySet = ks;
+     *   }
+     *   return ks;
+     * }
+     *}</pre>

and changed

--- a/src/java.base/share/classes/java/util/TreeMap.java	Thu Dec 17 20:42:01 2015 +0300
+++ b/src/java.base/share/classes/java/util/TreeMap.java	Thu Dec 17 21:14:58 2015 +0300
@@ -852,7 +852,11 @@
      */
     public Collection<V> values() {
         Collection<V> vs = values;
-        return (vs != null) ? vs : (values = new Values());
+        if (vs == null) {
+            vs = new Values();
+            values = vs;
+        }
+        return vs;
     }
 
     /**

but

    public NavigableSet<K> navigableKeySet() {
        KeySet<K> nks = navigableKeySet;
        return (nks != null) ? nks : (navigableKeySet = new KeySet<>(this));
    }

remained unchanged, with navigableKeySet also non-volatile, so I'd think
the comment above would apply. (Similar for entrySet() and descendingMap().)

What am I missing?

	vb

////////////////////////////////////////////////////////////////////

ATIS systems GmbH 
Sitz: Bad Homburg 
Registergericht: Bad Homburg HRB 1514 
Geschaeftsfuehrer: Stefan Diepolder, Daniel Sieh
 
________________________________________________________________________________________________

This message is confidential. It may also be privileged or otherwise protected by work product immunity or other legal rules. If you have received this message by mistake please let us know by reply and then delete it from your system; you should not copy it or disclose its contents to anyone. All messages sent to and from ATIS systems GmbH may be monitored to ensure compliance with internal policies and to protect our business. E-Mails are not secure and cannot be guaranteed to be error free as they can be intercepted, amended, lost or destroyed. Anyone who communicates with us by e-mail is taken to accept these risks.






More information about the Concurrency-interest mailing list