標籤:變數 fas lsa i++ block 資料 import list TE
先說結論:在用for遍曆一個集合對象時,如果遍曆過程中對集合對象的內容進行了修改(增加、刪除),則會拋出ConcurrentModificationException。在單線程下用迭代器遍曆修改,則不會報錯。在多線程環境下則會報錯。
??原理:迭代器在遍曆時直接存取集合中的內容,並且在遍曆過程中使用一個 modCount 變數。集合在被遍曆期間如果內容發生變化,就會改變modCount的值。每當迭代器使用hashNext()/next()遍曆下一個元素之前,都會檢測modCount變數是否為expectedmodCount值,是的話就返回遍曆;否則拋出異常,終止遍曆。
??這裡異常的拋出條件是檢測到 modCount!=expectedmodCount這個條件。如果集合發生變化時修改modCount值剛好又設定為了expectedmodCount值,則異常不會拋出。因此,不能依賴於這個異常是否拋出而進行並行作業的編程,這個異常只建議用於檢測並發修改的bug。
我們來檢測單線程下用增強for迴圈來更新資料會不會報錯:
import java.util.*;public static void main(String[] args) { List<Integer> list = new ArrayList<>(); for(int i=0;i<10;i++) { list.add(i); } for(int n:list){ if(n==6){ list.set(n, 16); } } System.out.println(list.toString());}
輸出是[0, 1, 2, 3, 4, 5, 16, 7, 8, 9]
那如果在遍曆中刪除參數呢?
public static void main(String[] args) { List<Integer> list = new ArrayList<>(); for(int i=0;i<10;i++) { list.add(i); } for(int n:list){ if(n==6){ list.remove(n); } } System.out.println(list.toString());}
->Exception in thread "main" java.util.ConcurrentModificationException
這是為什麼呢?讓我們來看看set的源碼:
public E set(int index, E element) { rangeCheck(index); E oldValue = elementData(index); elementData[index] = element; return oldValue;}
我們發現,進行set操作的時候,modCount並沒有自增,所以不會報錯。
那如果用迭代器進行遍曆remove呢?
public static void main(String[] args) { List<Integer> list = new ArrayList<>(); for(int i=0;i<10;i++) { list.add(i); } Iterator<Integer> it = list.iterator(); while (it.hasNext()) { if (it.next() == 6) { it.remove(); } } System.out.println(list.toString());}
->[0, 1, 2, 3, 4, 5, 7, 8, 9]
這是因為:
- 迭代器是作為當前集合的內部類實現的,當迭代器建立的時候保持了當前集合的引用;
- 集合內部維護一個int變數modCount,用來記錄集合被修改的次數,比如add,remove等都會使該欄位遞增;
- modCount這個參數記錄了某個List改變大小的次數,如果modCount改變的不符合預期,那麼就會拋出異常。
- 迭代器內部也維護著當前集合的修改次數的欄位,迭代器建立時該欄位初始化為集合的modCount值
- 當每一次迭代時,迭代器會比較迭代器維護的欄位和modCount的值是否相等,如果不相等就拋ConcurrentModifiedException異常;
- 當然,如果用迭代器調用remove方法,那麼集合和迭代器維護的修改次數都會遞增,以保持兩個狀態的一致。
如下面ArrayList繼承的AbstractList源碼所示:
protected transient int modCount = 0;
ArrayList源碼:
public void remove() { if (lastRet < 0) throw new IllegalStateException(); checkForComodification(); try { ArrayList.this.remove(lastRet); cursor = lastRet; lastRet = -1; expectedModCount = modCount; } catch (IndexOutOfBoundsException ex) { throw new ConcurrentModificationException(); }}public E remove(int index) { rangeCheck(index); modCount++; E oldValue = elementData(index); int numMoved = size - index - 1; if (numMoved > 0) System.arraycopy(elementData, index+1, elementData, index,numMoved); elementData[--size] = null; // clear to let GC do its work return oldValue;}private void rangeCheck(int index) { if (index >= size) throw new IndexOutOfBoundsException(outOfBoundsMsg(index));}
可以發現進行remove操作時變數modCount自增了
??Ps:java.util包下的集合類都是快速失敗的,不能在多線程下發生並發修改(迭代過程中被修改)。
安全失敗(fail—safe)
??採用安全失敗機制的集合容器,在遍曆時不是直接在集合內容上訪問的,而是先複製原有集合內容,在拷貝的集合上進行遍曆。
??原理:由於迭代時是對原集合的拷貝進行遍曆,所以在遍曆過程中對原集合所作的修改並不能被迭代器檢測到,所以不會觸發Concurrent Modification Exception。
??缺點:基於拷貝內容的優點是避免了Concurrent Modification Exception,但同樣地,迭代器並不能訪問到修改後的內容,即:迭代器遍曆的是開始遍曆那一刻拿到的集合拷貝,在遍曆期間原集合發生的修改迭代器是不知道的。
??Ps:java.util.concurrent包下的容器都是安全失敗,可以在多線程下並發使用,並發修改。
Java集合架構中的快速失敗(fail—fast)機制詳解