class Te1 extends Thread { private List<Integer> list; public Te1(List<Integer> list) { this.list = list; } public void run() { Iterator<Integer> iterator = list.iterator(); while(iterator.hasNext()){ int i = iterator.next(); } } } class Te2 extends Thread { private List<Integer> list; public Te2(List<Integer> list) { this.list = list; } public void run() { for (int i = 0; i < list.size(); i++) { list.remove(i); } } } public class Test { public static void main(String[] args) { ArrayList<Integer> list=new ArrayList(); for (int i = 0; i <100 ; i++) { list.add(i); } Te1 t1=new Te1(list); Te2 t2=new Te2(list); t1.start(); t2.start(); } }
736 public Iterator<E> iterator() { 737 return new Itr(); 738 } 743 private class Itr implements Iterator<E> { 744 int cursor; // index of next element to return 745 int lastRet = -1; // index of last element returned; -1 if no such 746 int expectedModCount = modCount; 747 748 public boolean hasNext() { 749 return cursor != size; 750 } 751 752 @SuppressWarnings("unchecked") 753 public E next() { 754 checkForComodification(); ... 763 } 764 765 public void remove() { 766 if (lastRet < 0) 767 throw new IllegalStateException(); 768 checkForComodification(); ... 778 } 779 780 final void checkForComodification() { 781 if (modCount != expectedModCount) 782 throw new ConcurrentModificationException(); 783 } 784 }
377 public boolean add(E e) { 378 ensureCapacity(size + 1); // Increments modCount!! ... 381 } 178 public void ensureCapacity(int minCapacity) { 179 modCount++; 180 ... 189 } 439 public boolean remove(Object o) { 440 if (o == null) { 441 for (int index = 0; index < size; index++) 442 if (elementData[index] == null) { 443 fastRemove(index); 444 return true; 445 } 446 } else { 447 for (int index = 0; index < size; index++) 448 if (o.equals(elementData[index])) { 449 fastRemove(index); 450 return true; 451 } 452 } 453 return false; 454 } 460 private void fastRemove(int index) { 461 modCount++; ... 467 }
A fail-fast system is nothing but immediately report any failure that is likely to lead to failure. When a problem occurs, a fail-fast system fails immediately. In Java, we can find this behavior with iterators. In case, you have called iterator on a collection object, and another thread tries to modify the collection object, then concurrent modification exception will be thrown. This is called fail-fast.
public class Test { public static void main(String[] args) { CopyOnWriteArrayList<Integer> list=new CopyOnWriteArrayList(); for (int i = 0; i <100 ; i++) { list.add(i); } Te1 t1=new Te1(list); Te2 t2=new Te2(list); t1.start(); t2.start(); }
469 public E remove(int index) { 470 final ReentrantLock lock = this.lock; 471 lock.lock(); 472 try { 473 Object[] elements = getArray(); 474 int len = elements.length; 475 E oldValue = get(elements, index); 476 int numMoved = len - index - 1; 477 if (numMoved == 0) 478 setArray(Arrays.copyOf(elements, len - 1)); 479 else { 480 Object[] newElements = new Object[len - 1]; 481 System.arraycopy(elements, 0, newElements, 0, index); 482 System.arraycopy(elements, index + 1, newElements, index, 483 numMoved); 484 setArray(newElements); 485 } 486 return oldValue; 487 } finally { 488 lock.unlock(); 489 } 490 } 99 final void setArray(Object[] a) { 100 array = a; 101 }
956 public Iterator<E> iterator() { 957 return new COWIterator<E>(getArray(), 0); 958 } 991 private static class COWIterator<E> implements ListIterator<E> { 992 993 private final Object[] snapshot; 994 995 private int cursor; 996 997 private COWIterator(Object[] elements, int initialCursor) { 998 cursor = initialCursor; 999 snapshot = elements; 1000 } 1001 1002 public boolean hasNext() { 1003 return cursor < snapshot.length; 1004 } 1005 1010 @SuppressWarnings("unchecked") 1011 public E next() { 1012 if (! hasNext()) 1013 throw new NoSuchElementException(); 1014 return (E) snapshot[cursor++]; 1015 } 1016
做者:jiajun 出處: http://www.cnblogs.com/-new/
本文版權歸做者和博客園共有,歡迎轉載,但未經做者贊成必須保留此段聲明,且在文章頁面明顯位置給出原文鏈接,不然保留追究法律責任的權利。若是以爲還有幫助的話,能夠點一下右下角的【推薦】,但願可以持續的爲你們帶來好的技術文章!想跟我一塊兒進步麼?那就【關注】我吧。html