Why Does Java HashMap modCount Vanish After JDK7? Exploring Fail‑Fast Mechanics
The article examines the role of the volatile modCount field in Java’s HashMap fail‑fast iterator, shows how its declaration changed from JDK5/6 to JDK7/8, and explains why ConcurrentModificationException can occur even in single‑threaded contexts.
Incorrect conclusion
When searching online for the purpose of the modCount variable in HashMap, most explanations describe the fail‑fast mechanism.
Fail‑Fast mechanism
We know that java.util.HashMap is not thread‑safe, so if another thread modifies the map while an iterator is in use, a ConcurrentModificationException is thrown—this is the fail‑fast strategy. In the source code this is implemented via the modCount field, which counts modifications. When an iterator is created, its expectedModCount is set to the current modCount. During iteration the iterator checks whether modCount equals expectedModCount; a mismatch indicates that the map has been modified, and the iterator throws the exception. Note that modCount is declared volatile to ensure visibility across threads.
This explanation was valid for JDK5 and JDK6, where modCount was indeed declared volatile . In JDK7 and JDK8 the declaration no longer includes volatile .
Below are source code snapshots for JDK5, JDK6, JDK7, and JDK8.
JDK5 source
JDK6 source
JDK7 source
JDK8 source
Reflection
Does JDK7/8 no longer need the modCount variable to prevent concurrent modifications during iteration?
My reasoning: the comment for modCount points us to ConcurrentModificationException. In the Javadoc of that exception we find the following description:
Note that this exception does not always indicate that an object has
been concurrently modified by a different thread. If a single
thread issues a sequence of method invocations that violates the
contract of an object, the object may throw this exception. For
example, if a thread modifies a collection directly while it is
iterating over the collection with a fail-fast iterator, the iterator
will throw this exception.Translation:
Note that this exception does not always indicate that an object has been concurrently modified by another thread. If a single thread performs a series of calls that violate the object's contract, the object may throw this exception. For example, if a thread modifies a collection while iterating over it with a fail‑fast iterator, the iterator will throw this exception.
From this description I conclude:
The exception can occur not only in multithreaded scenarios.
It can also be thrown in single‑threaded code when a fail‑fast iterator modifies the collection during iteration.
The modCount field in HashMap exists to support this behavior.
Java Interview Crash Guide
Dedicated to sharing Java interview Q&A; follow and reply "java" to receive a free premium Java interview guide.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
