All, I was trying to understand the multiple thread race condition issue for the Hashmap resize function.
As I read from here. The race condition issue will cause the infinite loop link for the entry list.
I already knew that Hashmap
has the fail-fast mechanism to immediately stop the multiple threads access it. The following code shows that.
if (modCount != expectedModCount)
throw new ConcurrentModificationException();
My question is why fail-fast doesn't work for the resize function? The code I debug is below.(jdk1.7)
void transfer(Entry[] newTable, boolean rehash) {
int newCapacity = newTable.length;
for (Entry<K,V> e : table) {
while(null != e) {
Entry<K,V> next = e.next;
if (rehash) {
e.hash = null == e.key ? 0 : hash(e.key);
}
int i = indexFor(e.hash, newCapacity);
e.next = newTable[i];
newTable[i] = e;
e = next;
}
}
}
Because for
doesn't use the Iterator
?
Updated
Or Because resize function doesn't use the Put*
, Remove*
, Clear*
method which will cause the modCount
value changed? Please help to confirm it.(Forgive me the bad English.)
Thanks.