modCount到底是干什么的呢
在ArrayList,LinkedList,HashMap等等的內部實現(xiàn)增,刪,改中我們總能看到modCount的身影,modCount字面意思就是修改次數(shù),但為什么要記錄modCount的修改次數(shù)呢?
大家發(fā)現(xiàn)一個公共特點沒有,所有使用modCount屬性的全是線程不安全的,這是為什么呢?說明這個玩意肯定和線程安全有關系嘍,那有什么關系呢
閱讀源碼,發(fā)現(xiàn)這玩意只有在本數(shù)據(jù)結構對應迭代器中才使用,以HashMap為例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
private abstract class HashIterator<E> implements Iterator<E> { Entry<K,V> next; // next entry to return int expectedModCount; // For fast-fail int index; // current slot Entry<K,V> current; // current entry HashIterator() { expectedModCount = modCount; if (size > 0 ) { // advance to first entry Entry[] t = table; while (index < t.length && (next = t[index++]) == null ) ; } } public final boolean hasNext() { return next != null ; } final Entry<K,V> nextEntry() { if (modCount != expectedModCount) throw new ConcurrentModificationException(); Entry<K,V> e = next; if (e == null ) throw new NoSuchElementException(); if ((next = e.next) == null ) { Entry[] t = table; while (index < t.length && (next = t[index++]) == null ) ; } current = e; return e; } public void remove() { if (current == null ) throw new IllegalStateException(); if (modCount != expectedModCount) throw new ConcurrentModificationException(); Object k = current.key; current = null ; HashMap. this .removeEntryForKey(k); expectedModCount = modCount; } } |
由以上代碼可以看出,在一個迭代器初始的時候會賦予它調用這個迭代器的對象的mCount,如何在迭代器遍歷的過程中,一旦發(fā)現(xiàn)這個對象的mcount和迭代器中存儲的mcount不一樣那就拋異常
好的,下面是這個的完整解釋
Fail-Fast 機制
我們知道 java.util.HashMap 不是線程安全的,因此如果在使用迭代器的過程中有其他線程修改了map,那么將拋出ConcurrentModificationException,這就是所謂fail-fast策略。這一策略在源碼中的實現(xiàn)是通過 modCount 域,modCount 顧名思義就是修改次數(shù),對HashMap 內容的修改都將增加這個值,那么在迭代器初始化過程中會將這個值賦給迭代器的 expectedModCount。在迭代過程中,判斷 modCount 跟 expectedModCount 是否相等,如果不相等就表示已經有其他線程修改了 Map:注意到 modCount 聲明為 volatile,保證線程之間修改的可見性。
所以在這里和大家建議,當大家遍歷那些非線程安全的數(shù)據(jù)結構時,盡量使用迭代器
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
原文鏈接:http://blog.csdn.net/u012926924/article/details/50452411