关于Java集合中fail-fast(快速失败)机制

82 阅读4分钟

这篇文章主要给大家介绍了关于Java集合中fail-fast(快速失败)机制的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧\

简介

我们知道Java中Collection接口下的很多集合都是线程不安全的, 比如 java.util.ArrayList不是线程安全的, 因此如果在使用迭代器的过程中有其他线程修改了list,那么将抛出ConcurrentModificationException,这就是所谓fail-fast策略。

这一策略在源码中的实现是通过 modCount 域,modCount 顾名思义就是修改次数,对ArrayList 内容的修改都将增加这个值,那么在迭代器初始化过程中会将这个值赋给迭代器的 expectedModCount。在迭代过程中,判断 modCount 跟 expectedModCount 是否相等,如果不相等就表示已经有其他线程修改了 list
注意到 modCount 声明为 volatile,保证线程之间修改的可见性。

modCount和expectedModCount

modCount和expectedModCount是用于表示修改次数的,其中modCount表示集合的修改次数,这其中包括了调用集合本身的add, remove, clear方法等修改方法时进行的修改和调用集合迭代器的修改方法进行的修改。而expectedModCount则是表示迭代器对集合进行修改的次数。

设置expectedModCount的目的就是要保证在使用迭代器期间,list对象只能有这一个迭代器对list进行修改。

在创建迭代器的时候会把对象的modCount的值传递给迭代器的expectedModCount:

12345private class ListItr ``implements ListIterator<E> {`` ``private Node<E> lastReturned;`` ``private Node<E> next;`` ``private int nextIndex;`` ``private int expectedModCount = modCount;

如果创建多个迭代器对一个集合对象进行修改的话,那么就会有一个modCount和多个expectedModCount,且modCount的值之间也会不一样,这就导致了moCount和expectedModCount的值不一致,从而产生异常:

12345678910public E next() {``  ``checkForComodification();``  ``if (!hasNext())``  ``throw new NoSuchElementException();``  ``lastReturned = next;``  ``next = next.next;``  ``nextIndex++;``  ``return lastReturned.item;`` ``}

上面的代码中的checkForComodification会检查modCount和expectedModCount的值是否一致,不一致则抛出异常。

1234final void checkForComodification() {`` ``if (modCount != expectedModCount)``  ``throw new ConcurrentModificationException();

modCount是如何被修改的

| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475 | // 添加元素到队列最后``public boolean add(E e) {`` ``// 修改modCount`` `` ensureCapacity(size + ``1`` ); ``// Increments modCount!!`` ``elementData[size++] = e;`` ``return true``;``}``// 添加元素到指定的位置``public void add(``int index, E element) {`` ``if (index > size || index < ``0``)``  ``throw new IndexOutOfBoundsException(``  ``"Index: "``+index+``", Size: "``+size);`` ``// 修改modCount`` ``ensureCapacity(size+``1`` ); ``// Increments modCount!!`` `` System.arraycopy(elementData, index, elementData, index + ``1``,``   ``size - index);`` ``elementData[index] = element;`` ``size++;``}``// 添加集合``public boolean addAll(Collection<? ``extends E> c) {`` ``Object[] a = c.toArray();`` ``int numNew = a.length;`` ``// 修改modCount`` `` ensureCapacity(size + numNew); ``// Increments modCount`` `` System.arraycopy(a, ``0``, elementData, size, numNew);`` ``size += numNew;`` ``return numNew != ``0``;``}``// 删除指定位置的元素``public E remove(``int index) {`` ``RangeCheck(index);`` ``// 修改modCount`` ``modCount++;`` ``E oldValue = (E) elementData[index];`` ``int numMoved = size - index - ``1``;`` ``if (numMoved > ``0``)``  ``System.arraycopy(elementData, index+``1``, elementData, index, numMoved);`` `` elementData[--size] = ``null`` ; ``// Let gc do its work`` ``return oldValue;``}``// 快速删除指定位置的元素``private void fastRemove(``int index) {`` ``// 修改modCount`` ``modCount++;`` ``int numMoved = size - index - ``1``;`` ``if (numMoved > ``0``)``  ``System.arraycopy(elementData, index+``1``, elementData, index,``       ``numMoved);`` `` elementData[--size] = ``null`` ; ``// Let gc do its work``}``// 清空集合``public void clear() {`` ``// 修改modCount`` ``modCount++;`` ``// Let gc do its work`` ``for (``int i = ``0``; i < size; i++)``  `` elementData[i] = ``null``;`` `` size = ``0``;``} |

也就是在对集合进行数据的增删的时候都会执行modcount++, 那么如果一个线程还在使用迭代器遍历这个list的时候就会发现异常, 发生 fail-fast(快速失败)

fail-fast(快速失败)和fail-safe(安全失败)比较

Iterator的快速失败是基于对底层集合做拷贝是浅拷贝,因此,它受源集合上修改的影响。java.util包下面的所有的集合类都是快速失败的

而java.util.concurrent包下面的所有的类都是使用锁实现安全失败的。

快速失败的迭代器会抛出ConcurrentModificationException异常,而安全失败的迭代器永远不会抛出这样的异常。

fail-fast解决什么问题

fail-fast机制,是一种错误检测机制。

它只能被用来检测错误,因为JDK并不保证fail-fast机制一定会发生。只是在多线程环境下告诉客户端发生了多线程安全问题.
所以若在多线程环境下使用fail-fast机制的集合,建议使用“java.util.concurrent包下的类”去取代“java.util包下的类”。

如何解决fail-fast事件

ArrayList对应的CopyOnWriteArrayList进行说明。我们先看看CopyOnWriteArrayList的源码:

| 123456789101112131415161718192021222324252627 | public class CopyOnWriteArrayList<E>`` ``implements List<E>, RandomAccess, Cloneable, java.io.Serializable {`` ``...`` ``// 返回集合对应的迭代器`` ``public Iterator<E> iterator() {``  ``return new COWIterator<E>(getArray(), ``0``);`` ``}`` ``...`` ``private static class COWIterator<E> ``implements ListIterator<E> {``  ``private final Object[] snapshot;``  ``private int cursor;``  ``private COWIterator(Object[] elements, ``int initialCursor) {``   ``cursor = initialCursor;``   ``// 新建COWIterator时,将集合中的元素保存到一个新的拷贝数组中。``   ``// 这样,当原始集合的数据改变,拷贝数据中的值也不会变化。``   ``snapshot = elements;``  ``}``  ``public boolean hasNext() {``   ``return cursor < snapshot.length;``  ``} |

CopyOnWriteArrayList是自己实现Iterator, 并且CopyOnWriteArrayList的Iterator实现类中,没有所谓的checkForComodification(),更不会抛出ConcurrentModificationException异常

CopyOnWriteArrayList在进行新建COWIterator时,将集合中的元素保存到一个新的拷贝数组中。这样,当原始集合的数据改变,拷贝数据中的值也不会变化。