Java容器的fail-fast fail-safe策略详细解读
创始人
2025-05-28 02:35:51
0

Java容器的fail-fast fail-safe策略详细解读

  • fail-fast
    • ArrayList
    • HashMap
    • 总结
  • fail-safe
    • CopyOnWriteArrayList
    • ConcurrentHashMap
    • 总结

fail-fast

在fail-fast中所有的集合容器都是强一致性的,因为他们在各种遍历之前,都会提取保存modCount的值,为后面每一次迭代或者遍历前进行比较,不一致则抛出并发修改异常。Collection以ArrayList为代表,Map以HashMap为代表进行验证

ArrayList

可以发现不管是forEach遍历还是iterator获取迭代器进行迭代也好,都会提前保存modCount的值,并且每次调用iterator()都会生成新的迭代器(每次都会记录当前ArrayList的modCout最新值);并且每次循环或者迭代都会判断modCount != expectedModCount,如果不一致则抛出并发修改异常ConcurrentModificationException

public class ArrayList extends AbstractListimplements List, RandomAccess, Cloneable, java.io.Serializable
{@Overridepublic void forEach(Consumer 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

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> 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 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 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(); }}}

总结

  1. fail-fast的容器是不允许在遍历或者迭代的时候修改值 ,每次指针下移的时候都会判断modCount != expectedModCount,如果不一致则抛出并发修改异常ConcurrentModificationException
  2. 迭代器一旦创建就会记录当前modCount的值,所以可能出现多个迭代器遍历出的结果不一样的情况
  3. 可以使用迭代器的remove方法删除元素,删除后会更新当前迭代器的modCount(很明显只允许当前迭代器删除数据,避免并发线程删除数据视图),ArrayList通过iterator()获取的迭代器只支持删除元素,通过listIterator()获取的迭代器还支持添加和修改元素
  4. map的keys、values、entrySet()都有foreach跟迭代器,都满足以上几点
  5. 并发修改异常ConcurrentModificationException只能用于检测并发修改时出现的bug,开发中不能够依赖这个异常是否抛出而进行并发操作的编程,如果有一个线程删除数据,有恰好有另一个线程添加了数据,那么modCout的值还是不变,并不能被某个线程迭代器所检测出来。
  6. fail-fast很明显在数据一致性跟可用性之间选择了数据一致性,当检测数据视图不一致的情况立马通过抛出异常中断当前迭代器指针迭代、for循环的遍历操作,保证数据的强一致性

fail-safe

在java.util.concurrent包下的容器全部都是fail-safe,它们允许在并发下修改数据,在很多网络资料中都说juc包下的容器不去检测并发修改异常,从而实现迭代时可以修改值的情况,这种说法是不够严谨的。通过CopyOnWriteArrayList、ConcurrentHashMap源码进行解析

CopyOnWriteArrayList

  1. CopyOnWriteArrayList的迭代器和for遍历并没有检测并发修改异常的操作,但是迭代器的set()、add()、remove()会抛出UnsupportedOperationException的异常,遍历只支持读操作,是读写分离思想的体现
  2. 可以使用copyOnWriteArrayList自身的增删改操作,增删改操作使用了类似String不可变类的思想,每次更新操作都会复制一份出来并替换原来的Object数组,不会影响到创建迭代器时拿到的Object数组的迭代遍历,写操作也不会阻塞读操作
  3. CopyOnWriteArrayList是弱一致性的,允许迭代时修改数据,在数据的一致性和可用性中选择了可用性。
public class CopyOnWriteArrayListimplements List, RandomAccess, Cloneable, java.io.Serializable {// foreach中并没有出现任何检测并发修改的操作public void forEach(Consumer 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();}}
}

ConcurrentHashMap

  1. ConcurrentHashMap的keys()、values()、entrySet()同样也没有检测并发修改的操作
  2. keySet()、entrySet()支持添加删除操作,values()支持删除不支持添加操作
  3. ConcurrentHashMap中节点的val和next都是volatile修饰的,如果变化发生在已遍历过的部分,迭代器就不会反映出来,而如果变化发生在未遍历过的部分,迭代器就会发现并反映出来,这就是弱一致性
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);}
}

总结

  • JUC包下的容器都是fail-safe,并且都是在数据的一致性跟可用性中选择了可用性,允许出现数据的短期不一致,但是保证最终数据的一致性
  • 并发容器遍历的策略,有的在并发修改时的数据不能再迭代器中遍历出来,如CopyOnWriteArrayList通过读写分离,在遍历的时候保证读视图不受并发修改的影响,有的通过volatile保证数据多线程的可见性如ConcurrentHashMap。

以上便是Java容器的fail-fast fail-safe策略详细解读,仅为个人见解,如有不当欢迎在评论区交流!

相关内容

热门资讯

扫房神器2安卓系统,打造洁净家... 你有没有发现,家里的灰尘就像小精灵一样,总是悄悄地在你不注意的时候跳出来?别急,今天我要给你介绍一个...
安卓完整的系统设置,全面掌控手... 亲爱的手机控们,是不是觉得你的安卓手机用久了,功能越来越强大,但设置却越来越复杂?别急,今天就来带你...
电视安卓系统是几代机子,揭秘新... 你有没有想过,家里的电视是不是已经升级到了最新的安卓系统呢?别小看了这个小小的系统升级,它可是能让你...
安卓系统隐私有经常去,系统级防... 你知道吗?在咱们这个数字化时代,手机可是我们生活中不可或缺的好伙伴。但是,你知道吗?这个好伙伴有时候...
安卓10系统断网软件,轻松实现... 你有没有遇到过这种情况?手机突然断网了,明明信号满格,却连不上网,急得你团团转。别急,今天就来给你揭...
安卓可以改什么系统版本,体验全... 你有没有想过,你的安卓手机其实可以像换衣服一样,换一个全新的“系统版本”呢?没错,这就是今天我们要聊...
最好的平板游戏安卓系统,畅享指... 亲爱的游戏迷们,你是否在寻找一款能够让你在安卓平板上畅玩无忧的游戏神器?别急,今天我就要给你揭秘,究...
华为安卓系统卡顿解决,华为安卓... 你是不是也遇到了华为安卓系统卡顿的问题?别急,今天就来给你支几招,让你的华为手机重新焕发活力!一、清...
安卓建议升级鸿蒙系统吗,探讨鸿... 亲爱的安卓用户们,最近是不是被鸿蒙系统的新鲜劲儿给吸引了?是不是在犹豫要不要把你的安卓手机升级成鸿蒙...
安卓如何变苹果系统桌面,桌面系... 你有没有想过,把你的安卓手机变成苹果系统桌面,是不是瞬间高大上了呢?想象那流畅的动画效果,那简洁的界...
windows平板安卓系统升级... 你有没有发现,最近你的Windows平板电脑突然变得有些不一样了?没错,就是那个一直默默陪伴你的小家...
安卓系统扩大运行内存,解锁更大... 你知道吗?在科技飞速发展的今天,手机已经成为了我们生活中不可或缺的好伙伴。而手机中,安卓系统更是以其...
安卓系统怎么改变zenly,探... 你有没有发现,你的安卓手机上的Zenly应用最近好像变得不一样了?没错,安卓系统的大手笔更新,让Ze...
英特尔安卓子系统,引领高效移动... 你有没有想过,手机里的安卓系统竟然也能和电脑上的英特尔处理器完美结合呢?这可不是天方夜谭,而是科技发...
永远会用安卓系统的手机,探索安... 亲爱的手机控们,你是否也有那么一款手机,它陪伴你度过了无数个日夜,成为了你生活中不可或缺的一部分?没...
有哪些安卓手机系统好用,好用系... 你有没有发现,现在手机市场上安卓手机的品牌和型号真是琳琅满目,让人挑花了眼?不过别急,今天我就来给你...
卡片记账安卓系统有吗,便捷财务... 你有没有想过,用手机记账是不是比拿着小本本记录来得方便多了?现在,手机上的应用层出不穷,那么,有没有...
武汉摩尔影城安卓系统APP,便... 你有没有想过,一部手机就能带你走进电影的世界,享受大屏幕带来的震撼?今天,就让我带你详细了解武汉摩尔...
联想刷安卓p系统,畅享智能新体... 你有没有发现,最近联想的安卓P系统刷机热潮可是席卷了整个互联网圈呢!这不,我就迫不及待地来和你聊聊这...
mac从安卓系统改成双系统,双... 你有没有想过,你的Mac电脑从安卓系统改成双系统后,生活会有哪些翻天覆地的变化呢?想象一边是流畅的苹...