在fail-fast中所有的集合容器都是强一致性的,因为他们在各种遍历之前,都会提取保存modCount的值,为后面每一次迭代或者遍历前进行比较,不一致则抛出并发修改异常。Collection以ArrayList为代表,Map以HashMap为代表进行验证
可以发现不管是forEach遍历还是iterator获取迭代器进行迭代也好,都会提前保存modCount的值,并且每次调用iterator()都会生成新的迭代器(每次都会记录当前ArrayList的modCout最新值);并且每次循环或者迭代都会判断modCount != expectedModCount,如果不一致则抛出并发修改异常ConcurrentModificationException
public class ArrayList extends AbstractListimplements List, RandomAccess, Cloneable, java.io.Serializable
{@Overridepublic void forEach(Consumer super E> action) {Objects.requireNonNull(action);final int expectedModCount = modCount; // 记录值@SuppressWarnings("unchecked")final E[] elementData = (E[]) this.elementData;final int size = this.size;for (int i=0; modCount == expectedModCount && i < size; i++) {action.accept(elementData[i]);}if (modCount != expectedModCount) {throw new ConcurrentModificationException();}}public Iterator iterator() {return new Itr();}private class Itr implements Iterator {int cursor; // index of next element to returnint lastRet = -1; // index of last element returned; -1 if no suchint expectedModCount = modCount; // 记录值Itr() {}public boolean hasNext() {return cursor != size;}@SuppressWarnings("unchecked")public E next() {checkForComodification();int i = cursor;if (i >= size)throw new NoSuchElementException();Object[] elementData = ArrayList.this.elementData;if (i >= elementData.length)throw new ConcurrentModificationException();cursor = i + 1;return (E) elementData[lastRet = i];}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();}}final void checkForComodification() {if (modCount != expectedModCount)throw new ConcurrentModificationException();}}
}
HashMap迭代遍历主要有entrySet()、keys()、values()
public class HashMap extends AbstractMapimplements Map, Cloneable, Serializable {final class EntryIterator extends HashIteratorimplements Iterator> {public final Map.Entry next() { return nextNode(); }}final class EntrySet extends AbstractSet> {public final int size() { return size; }public final void clear() { HashMap.this.clear(); }// entrySet 迭代器public final Iterator> iterator() {return new EntryIterator();}// entrySet foreachpublic final void forEach(Consumer super Map.Entry> action) {Node[] tab;if (action == null)throw new NullPointerException();if (size > 0 && (tab = table) != null) {int mc = modCount; // 记录值for (int i = 0; i < tab.length; ++i) {for (Node e = tab[i]; e != null; e = e.next)action.accept(e);}if (modCount != mc) // 检测并发修改异常throw new ConcurrentModificationException();}}}final class KeySet extends AbstractSet {// 迭代器public final Iterator iterator() { return new KeyIterator(); }// KeySet foreachpublic final void forEach(Consumer super K> action) {Node[] tab;if (action == null)throw new NullPointerException();if (size > 0 && (tab = table) != null) {int mc = modCount; // 记录值for (int i = 0; i < tab.length; ++i) {for (Node e = tab[i]; e != null; e = e.next)action.accept(e.key);}if (modCount != mc) // 检测并发修改异常throw new ConcurrentModificationException();}}}final class Values extends AbstractCollection {// 迭代器public final Iterator iterator() { return new ValueIterator(); }// values foreachpublic final void forEach(Consumer super V> action) {Node[] tab;if (action == null)throw new NullPointerException();if (size > 0 && (tab = table) != null) {int mc = modCount; // 记录值for (int i = 0; i < tab.length; ++i) {for (Node e = tab[i]; e != null; e = e.next)action.accept(e.value);}if (modCount != mc) // 检测并发修改异常throw new ConcurrentModificationException();}}}// 迭代器abstract class HashIterator {Node next; // next entry to returnNode current; // current entryint expectedModCount; // for fast-failint index; // current slotHashIterator() {expectedModCount = modCount; // 记录值Node[] t = table;current = next = null;index = 0;if (t != null && size > 0) { // advance to first entrydo {} while (index < t.length && (next = t[index++]) == null);}}public final boolean hasNext() {return next != null;}final Node nextNode() {Node[] t;Node e = next;if (modCount != expectedModCount) // entryset、values、keys 使用的迭代器都有并发修改异常检测throw new ConcurrentModificationException();if (e == null)throw new NoSuchElementException();if ((next = (current = e).next) == null && (t = table) != null) {do {} while (index < t.length && (next = t[index++]) == null);}return e;}public final void remove() {Node p = current;if (p == null)throw new IllegalStateException();if (modCount != expectedModCount)throw new ConcurrentModificationException();current = null;K key = p.key;removeNode(hash(key), key, null, false, false);expectedModCount = modCount; // 迭代器移除节点时会更新构造方法中的记录值}}final class KeyIterator extends HashIteratorimplements Iterator {public final K next() { return nextNode().key; }}final class ValueIterator extends HashIteratorimplements Iterator {public final V next() { return nextNode().value; }}final class EntryIterator extends HashIteratorimplements Iterator> {public final Map.Entry next() { return nextNode(); }}}
在java.util.concurrent包下的容器全部都是fail-safe,它们允许在并发下修改数据,在很多网络资料中都说juc包下的容器不去检测并发修改异常,从而实现迭代时可以修改值的情况,这种说法是不够严谨的。通过CopyOnWriteArrayList、ConcurrentHashMap源码进行解析
public class CopyOnWriteArrayListimplements List, RandomAccess, Cloneable, java.io.Serializable {// foreach中并没有出现任何检测并发修改的操作public void forEach(Consumer super E> action) {if (action == null) throw new NullPointerException();Object[] elements = getArray();int len = elements.length;for (int i = 0; i < len; ++i) {@SuppressWarnings("unchecked") E e = (E) elements[i];action.accept(e);}}public Iterator iterator() {return new COWIterator(getArray(), 0);}static final class COWIterator implements ListIterator {/** Snapshot of the array */private final Object[] snapshot;/** Index of element to be returned by subsequent call to next. */private int cursor;private COWIterator(Object[] elements, int initialCursor) {cursor = initialCursor;snapshot = elements;}public boolean hasNext() {return cursor < snapshot.length;}public boolean hasPrevious() {return cursor > 0;}// 迭代器中也没有检测并发修改操作@SuppressWarnings("unchecked")public E next() {if (! hasNext())throw new NoSuchElementException();return (E) snapshot[cursor++];}@SuppressWarnings("unchecked")public E previous() {if (! hasPrevious())throw new NoSuchElementException();return (E) snapshot[--cursor];}public int nextIndex() {return cursor;}public int previousIndex() {return cursor-1;}/*** Not supported. Always throws UnsupportedOperationException.* @throws UnsupportedOperationException always; {@code remove}* is not supported by this iterator.*/public void remove() {throw new UnsupportedOperationException();}/*** Not supported. Always throws UnsupportedOperationException.* @throws UnsupportedOperationException always; {@code set}* is not supported by this iterator.*/public void set(E e) {throw new UnsupportedOperationException();}/*** Not supported. Always throws UnsupportedOperationException.* @throws UnsupportedOperationException always; {@code add}* is not supported by this iterator.*/public void add(E e) {throw new UnsupportedOperationException();}}
}
static final class KeyIterator extends BaseIteratorimplements Iterator, Enumeration {KeyIterator(Node[] tab, int index, int size, int limit,ConcurrentHashMap map) {super(tab, index, size, limit, map);}public final K next() {Node p;if ((p = next) == null)throw new NoSuchElementException();K k = p.key;lastReturned = p;advance();return k;}public final K nextElement() { return next(); }
}static final class ValueIterator extends BaseIteratorimplements Iterator, Enumeration {ValueIterator(Node[] tab, int index, int size, int limit,ConcurrentHashMap map) {super(tab, index, size, limit, map);}public final V next() {Node p;if ((p = next) == null)throw new NoSuchElementException();V v = p.val;lastReturned = p;advance();return v;}public final V nextElement() { return next(); }
}static final class EntryIterator extends BaseIteratorimplements Iterator> {EntryIterator(Node[] tab, int index, int size, int limit,ConcurrentHashMap map) {super(tab, index, size, limit, map);}public final Map.Entry next() {Node p;if ((p = next) == null)throw new NoSuchElementException();K k = p.key;V v = p.val;lastReturned = p;advance();return new MapEntry(k, v, map);}
}
以上便是Java容器的fail-fast fail-safe策略详细解读,仅为个人见解,如有不当欢迎在评论区交流!