Bug 36876 - HashMap and Hashtable fields missing volatile declaration
Summary: HashMap and Hashtable fields missing volatile declaration
Status: RESOLVED WONTFIX
Alias: None
Product: classpath
Classification: Unclassified
Component: classpath (show other bugs)
Version: 0.97.2
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2008-07-19 17:19 UTC by Laurence Hellyer
Modified: 2008-11-16 01:22 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Laurence Hellyer 2008-07-19 17:19:37 UTC
Hi,

Reviewing the code for java.util.HashMap and java.util.Hashtable it struck me that the field modCount should be declared volatile - otherwise a update of the field by another thread might not detected due to a lack of a suitable memory barrier.

This apparent omission came to light when comparing the number of classes containing volatile fields between openjdk and classpath.  Java-6-openjdk contains 151 classes that contain or extend a class with a volatile field.  Classpath 0.97.2 contains only 26 classes that contain a volatile field (0 classes extend a class containing a volatile field).  FYI the modCount field in openjdk's HashMap (that performs the same purpose) is declared volatile.
Comment 1 Ismael Juma 2008-07-31 13:46:17 UTC
This was discussed on IRC, but just wanted to record here the fact that OpenJDK no longer includes a volatile modifier in the modcount field in java.util.HashMap. This change was committed to the jdk7 mercurial repo a few months ago and I believe the motivation was that the fail-fast behaviour does not provide guarantees and code that accesses HashMap from a single-thread should not pay the price of the volatile read and write.
Comment 2 Andrew John Hughes 2008-11-16 01:22:04 UTC
Closing based on Ismael's comment.