JDK-8145539 : (coll) AbstractMap.keySet and .values should not be volatile
  • Type: Enhancement
  • Component: core-libs
  • Sub-Component: java.util:collections
  • Affected Version: 8u66,9
  • Priority: P4
  • Status: Resolved
  • Resolution: Fixed
  • OS: generic
  • CPU: generic
  • Submitted: 2015-12-16
  • Updated: 2023-10-23
  • Resolved: 2015-12-17
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 8 JDK 9
8u101Fixed 9 b99Fixed
Related Reports
Relates :  
Relates :  
Description
A DESCRIPTION OF THE REQUEST :
AbstractMap has two fields which are declared volatile:

public abstract class AbstractMap<K,V> implements Map<K,V> {
.....
    /**
     * Each of these fields are initialized to contain an instance of the
     * appropriate view the first time this view is requested.  The views are
     * stateless, so there's no reason to create more than one of each.
     */
    transient volatile Set<K>        keySet;
    transient volatile Collection<V> values;
....
} 

This is unnecessary since there is nothing in the API requiring these fields to be volatile.

JUSTIFICATION :
Removing volatile will reduce the cost of creating instances extending AbstractMap, as well as invoking e.g. ::values() and ::keySet() on a HashMap. In the application I'm working in hundreds of thousands HashMaps are created each second.



Comments
URL: http://hg.openjdk.java.net/jdk9/jdk9/jdk/rev/ce72c7641f38 User: lana Date: 2015-12-23 23:04:19 +0000
23-12-2015

URL: http://hg.openjdk.java.net/jdk9/dev/jdk/rev/ce72c7641f38 User: shade Date: 2015-12-17 22:45:16 +0000
17-12-2015

RFR: http://mail.openjdk.java.net/pipermail/core-libs-dev/2015-December/037674.html
16-12-2015

It is probably not that bad for HashMap initialization after JDK-8035284, but acquiring keySet/values should experience problems.
16-12-2015

This seems to predate OpenJDK: http://hg.openjdk.java.net/jdk9/jdk9/jdk/file/37a05a11f281/src/share/classes/java/util/AbstractMap.java#l294 I agree with submitter, there seem to be no obvious reason to keep these fields volatile. Concurrent collections are expected to override values() and keySet() with their own thread-safe implementations.
16-12-2015

Moving across to dev-team for evaluation.
16-12-2015