United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
Bug ID: JDK-6625725 (coll) modCount should not be volatile
JDK-6625725 : (coll) modCount should not be volatile

Details
Type:
Bug
Submit Date:
2007-11-04
Status:
Closed
Updated Date:
2012-10-08
Project Name:
JDK
Resolved Date:
2011-05-18
Component:
core-libs
OS:
generic
Sub-Component:
java.util:collections
CPU:
generic
Priority:
P3
Resolution:
Fixed
Affected Versions:
7
Fixed Versions:

Related Reports

Sub Tasks

Description
Collections use a modCount field to track changes to data structures,
so they can throw ConcurrentModificationException if another thread
unexpectedly modifies the collection (rather than corrupting the data
and carrying on).

Some collections mark modCount as volatile; some do not.
Since the modCount mechanism comes with no guarantees, and users of
single-threaded collections do not expect to pay the (high) price
of a volatile write on every modification operation, the volatile
modifier should be removed.

Here are the uses of volatile to be removed:

./java/util/HashMap.java:177:    transient volatile int modCount;
./java/util/IdentityHashMap.java:176:    private transient volatile int modCount;
./java/util/WeakHashMap.java:185:    volatile int modCount;

                                    

Comments
EVALUATION

Here's the obvious patch:

diff --git a/src/share/classes/java/util/HashMap.java b/src/share/classes/java/util/HashMap.java
--- a/src/share/classes/java/util/HashMap.java
+++ b/src/share/classes/java/util/HashMap.java
@@ -173,7 +173,7 @@ public class HashMap<K,V>
      * rehash).  This field is used to make iterators on Collection-views of
      * the HashMap fail-fast.  (See ConcurrentModificationException).
      */
-    transient volatile int modCount;
+    transient int modCount;
 
     /**
      * Constructs an empty <tt>HashMap</tt> with the specified initial
diff --git a/src/share/classes/java/util/IdentityHashMap.java b/src/share/classes/java/util/IdentityHashMap.java
--- a/src/share/classes/java/util/IdentityHashMap.java
+++ b/src/share/classes/java/util/IdentityHashMap.java
@@ -173,7 +173,7 @@ public class IdentityHashMap<K,V>
     /**
      * The number of modifications, to support fast-fail iterators
      */
-    private transient volatile int modCount;
+    private transient int modCount;
 
     /**
      * The next size value at which to resize (capacity * load factor).
diff --git a/src/share/classes/java/util/WeakHashMap.java b/src/share/classes/java/util/WeakHashMap.java
--- a/src/share/classes/java/util/WeakHashMap.java
+++ b/src/share/classes/java/util/WeakHashMap.java
@@ -181,7 +181,7 @@ public class WeakHashMap<K,V>
      *
      * @see ConcurrentModificationException
      */
-    volatile int modCount;
+    int modCount;
 
     @SuppressWarnings("unchecked")
     private Entry<K,V>[] newTable(int n) {
                                     
2008-03-06
EVALUATION

Yes.
                                     
2007-11-04



Hardware and Software, Engineered to Work Together