Java Solaris Communities Sun Store Join SDN My Profile Why Join?
 
Bug Database
Bug Detail
Quick Lists
Top 25 Bugs
Top 25 RFE's
Recently Closed Bugs
Printable Page Printable Page


Bug Database
Bug ID: 6625725
Votes 0
Synopsis (coll) modCount should not be volatile
Category java:classes_util
Reported Against
Release Fixed 7(b25)
State 10-Fix Delivered, bug
Priority: 3-Medium
Related Bugs
Submit Date 04-NOV-2007
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;
Posted Date : 2007-11-04 18:21:01.0
Work Around
N/A
Evaluation
Yes.
Posted Date : 2007-11-04 18:21:01.0

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) {
Posted Date : 2008-03-06 23:27:23.0
Comments
  
  Include a link with my name & email   


PLEASE NOTE: JDK6 is formerly known as Project Mustang