Java 集合系列-第四篇-fail-fast简介

fail-fast简介

fail-fast 机制是java集合(Collection)中的一种错误机制。当多个线程对同一个集合的内容进行操做时,就可能会产生fail-fast事件。 例如:当某一个线程A经过iterator去遍历某集合的过程当中,若该集合的内容被其余线程所改变了;那么线程A访问集合时,就会抛出ConcurrentModificationException异常,产生fail-fast事件。java

fail-fast示例

import java.util.*;
import java.util.concurrent.*;

/*
 * @desc java集合中Fast-Fail的测试程序。
 *
 *   fast-fail事件产生的条件:当多个线程对Collection进行操做时,若其中某一个线程经过iterator去遍历集合时,该集合的内容被其余线程所改变;则会抛出ConcurrentModificationException异常。
 *   fast-fail解决办法:经过util.concurrent集合包下的相应类去处理,则不会产生fast-fail事件。
 *
 *   本例中,分别测试ArrayList和CopyOnWriteArrayList这两种状况。ArrayList会产生fast-fail事件,而CopyOnWriteArrayList不会产生fast-fail事件。
 *   (01) 使用ArrayList时,会产生fast-fail事件,抛出ConcurrentModificationException异常;定义以下:
 *            private static List<String> list = new ArrayList<String>();
 *   (02) 使用时CopyOnWriteArrayList,不会产生fast-fail事件;定义以下:
 *            private static List<String> list = new CopyOnWriteArrayList<String>();
 *
 * @author skywang
 */
public class FastFailTest {

    private static List<String> list = new ArrayList<String>();
    //private static List<String> list = new CopyOnWriteArrayList<String>();
    public static void main(String[] args) {
    
        // 同时启动两个线程对list进行操做!
        new ThreadOne().start();
        new ThreadTwo().start();
    }

    private static void printAll() {
        System.out.println("");
		//这种遍历方式会发生错误,快速遍历方式不会发生错误。
        String value = null;
        Iterator iter = list.iterator();
        while(iter.hasNext()) {
            value = (String)iter.next();
            System.out.print(value+", ");
        }
		/**
		这种方式也会发生错误
		for (String i :list){
            System.out.println(i);
        }
		**/
		
		/**
		for (int i =0;i<size;i++){
            System.out.println(list.get(i).toString());
        }
		这种方式不会发生错误,必定能遍历对
		
		for (int i =0;i<size;i++){
            System.out.println(list.get(i).toString());
        }
		*/
    }

    /**
     * 向list中依次添加0,1,2,3,4,5,每添加一个数以后,就经过printAll()遍历整个list
     */
    private static class ThreadOne extends Thread {
        public void run() {
            int i = 0;
            while (i<6) {
                list.add(String.valueOf(i));
                printAll();
                i++;
            }
        }
    }

    /**
     * 向list中依次添加10,11,12,13,14,15,每添加一个数以后,就经过printAll()遍历整个list
     */
    private static class ThreadTwo extends Thread {
        public void run() {
            int i = 10;
            while (i<16) {
                list.add(String.valueOf(i));
                printAll();
                i++;
            }
        }
    }

}

运行该代码,抛出异常java.util.ConcurrentModificationException!即,产生fail-fast事件!数组

  • FastFailTest中经过 new ThreadOne().start() 和 new ThreadTwo().start() 同时启动两个线程去操做list。多线程

    ThreadOne线程:向list中依次添加0,1,2,3,4,5。每添加一个数以后,就经过printAll()遍历整个list。app

    ThreadTwo线程:向list中依次添加10,11,12,13,14,15。每添加一个数以后,就经过printAll()遍历整个list。dom

  • 当某一个线程遍历list的过程当中,list的内容被另一个线程所改变了;就会抛出ConcurrentModificationException异常,产生fail-fast事件。函数

fail-fast解决办法

fail-fast机制,是一种错误检测机制。它只能被用来检测错误,由于JDK并不保证fail-fast机制必定会发生。若在多线程环境下使用fail-fast机制的集合,建议使用“java.util.concurrent包下的类”去取代“java.util包下的类”。 因此,本例中只须要将ArrayList替换成java.util.concurrent包下对应的类便可。 即,将代码测试

private static List<String> list = new ArrayList<String>();

替换为this

private static List<String> list = new CopyOnWriteArrayList<String>();

fail-fast原理

产生fail-fast事件,是经过抛出ConcurrentModificationException异常来触发的。 那么,ArrayList是如何抛出ConcurrentModificationException异常的呢?线程

咱们知道,ConcurrentModificationException是在操做Iterator时抛出的异常。咱们先看看Iterator的源码。ArrayList的Iterator是在父类AbstractList.java中实现的。代码以下:code

package java.util;

public abstract class AbstractList<E> extends AbstractCollection<E> implements List<E> {

    ...

    // AbstractList中惟一的属性
    // 用来记录List修改的次数:每修改一次(添加/删除等操做),将modCount+1
    protected transient int modCount = 0;

    // 返回List对应迭代器。实际上,是返回Itr对象。
    public Iterator<E> iterator() {
        return new Itr();
    }

    // Itr是Iterator(迭代器)的实现类
    private class Itr implements Iterator<E> {
        int cursor = 0;

        int lastRet = -1;

        // 修改数的记录值。
        // 每次新建Itr()对象时,都会保存新建该对象时对应的modCount;
        // 之后每次遍历List中的元素的时候,都会比较expectedModCount和modCount是否相等;
        // 若不相等,则抛出ConcurrentModificationException异常,产生fail-fast事件。
        int expectedModCount = modCount;

        public boolean hasNext() {
            return cursor != size();
        }

        public E next() {
            // 获取下一个元素以前,都会判断“新建Itr对象时保存的modCount”和“当前的modCount”是否相等;
            // 若不相等,则抛出ConcurrentModificationException异常,产生fail-fast事件。
            checkForComodification();
            try {
                E next = get(cursor);
                lastRet = cursor++;
                return next;
            } catch (IndexOutOfBoundsException e) {
                checkForComodification();
                throw new NoSuchElementException();
            }
        }

        public void remove() {
            if (lastRet == -1)
                throw new IllegalStateException();
            checkForComodification();

            try {
                AbstractList.this.remove(lastRet);
                if (lastRet < cursor)
                    cursor--;
                lastRet = -1;
                expectedModCount = modCount;
            } catch (IndexOutOfBoundsException e) {
                throw new ConcurrentModificationException();
            }
        }

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

    ...
}

从中,咱们能够发如今调用 next() 和 remove()时,都会执行 checkForComodification()。若 “modCount 不等于 expectedModCount”,则抛出ConcurrentModificationException异常,产生fail-fast事件。

要搞明白 fail-fast机制,咱们就要须要理解何时“modCount 不等于 expectedModCount”! 从Itr类中,咱们知道 expectedModCount 在建立Itr对象时,被赋值为 modCount。经过Itr,咱们知道:expectedModCount不可能被修改成不等于 modCount。因此,须要考证的就是modCount什么时候会被修改。

arraylist 源码get方法

/**
     * Returns the element at the specified position in this list.
     *
     * @param  index index of the element to return
     * @return the element at the specified position in this list
     * @throws IndexOutOfBoundsException {@inheritDoc}
     */
    public E get(int index) {
        rangeCheck(index);

        return elementData(index);
    }
	  /**
     * Checks if the given index is in range.  If not, throws an appropriate
     * runtime exception.  This method does *not* check if the index is
     * negative: It is always used immediately prior to an array access,
     * which throws an ArrayIndexOutOfBoundsException if index is negative.
     */
    private void rangeCheck(int index) {
        if (index >= size)
            throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
    }

所以采用快速访问的方式,是不会抛出异常的。

接下来,咱们查看ArrayList的源码,来看看modCount是如何被修改的。

package java.util;

public class ArrayList<E> extends AbstractList<E>
        implements List<E>, RandomAccess, Cloneable, java.io.Serializable
{

    ...

    // list中容量变化时,对应的同步函数
    public void ensureCapacity(int minCapacity) {
        modCount++;
        int oldCapacity = elementData.length;
        if (minCapacity > oldCapacity) {
            Object oldData[] = elementData;
            int newCapacity = (oldCapacity * 3)/2 + 1;
            if (newCapacity < minCapacity)
                newCapacity = minCapacity;
            // minCapacity is usually close to size, so this is a win:
            elementData = Arrays.copyOf(elementData, newCapacity);
        }
    }


    // 添加元素到队列最后
    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;
    }

    ...
}

从中,咱们发现:不管是add()、remove(),仍是clear(),只要涉及到修改集合中的元素个数时,都会改变modCount的值。

在某一时刻,“线程a”建立了arrayList的Iterator。此时“节点A”仍然存在于arrayList中,建立arrayList时,expectedModCount = modCount(假设它们此时的值为N)。 在“线程a”在遍历arrayList过程当中的某一时刻,“线程b”执行了,而且“线程b”删除了arrayList中的“节点A”。“线程b”执行remove()进行删除操做时,在remove()中执行了“modCount++”,此时modCount变成了N+1! “线程a”接着遍历,当它执行到next()函数时,调用checkForComodification()比较“expectedModCount”和“modCount”的大小;而“expectedModCount=N”,“modCount=N+1”,这样,便抛出ConcurrentModificationException异常,产生fail-fast事件。

至此,咱们就彻底了解了fail-fast是如何产生的! 即,当多个线程对同一个集合进行操做的时候,某线程访问集合的过程当中,该集合的内容被其余线程所改变(即其它线程经过add、remove、clear等方法,改变了modCount的值);这时,就会抛出ConcurrentModificationException异常,产生fail-fast事件。

解决fail-fast的原理

那么java.util.concurrent包中是如何解决fail-fast事件的。

仍是以和ArrayList对应的CopyOnWriteArrayList进行说明。咱们先看看CopyOnWriteArrayList的源码:

package java.util.concurrent;
import java.util.*;
import java.util.concurrent.locks.*;
import sun.misc.Unsafe;

public class CopyOnWriteArrayList<E>
    implements List<E>, RandomAccess, Cloneable, java.io.Serializable {
    private static final long serialVersionUID = 8673264195747942595L;

    /** The lock protecting all mutators */
    transient final ReentrantLock lock = new ReentrantLock();

    /** The array, accessed only via getArray/setArray. */
    private volatile transient Object[] array;
    ...
	/**
     * Appends the specified element to the end of this list.
     *
     * @param e element to be appended to this list
     * @return <tt>true</tt> (as specified by {@link Collection#add})
     */
    public boolean add(E e) {
        final ReentrantLock lock = this.lock;
        lock.lock();
        try {
            Object[] elements = getArray();
            int len = elements.length;
            Object[] newElements = Arrays.copyOf(elements, len + 1);
            newElements[len] = e;
            setArray(newElements);
            return true;
        } finally {
            lock.unlock();
        }
    }
	/**
     * Sets the array.
     */
    final void setArray(Object[] a) {
        array = a;
    }
    // 返回集合对应的迭代器
    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;
        }

        public boolean hasPrevious() {
            return cursor > 0;
        }

        public E next() {
            if (! hasNext())
                throw new NoSuchElementException();
            return (E) snapshot[cursor++];
        }

        public E previous() {
            if (! hasPrevious())
                throw new NoSuchElementException();
            return (E) snapshot[--cursor];
        }

        public int nextIndex() {
            return cursor;
        }

        public int previousIndex() {
            return cursor-1;
        }

        public void remove() {
            throw new UnsupportedOperationException();
        }

        public void set(E e) {
            throw new UnsupportedOperationException();
        }

        public void add(E e) {
            throw new UnsupportedOperationException();
        }
    }
  
    ...

}
  • ArrayList继承于AbstractList不一样,CopyOnWriteArrayList没有继承于AbstractList,它仅仅只是实现了List接口。

  • ArrayList的iterator()函数返回的Iterator是在AbstractList中实现的;而CopyOnWriteArrayList是本身实现Iterator。

  • ArrayList的Iterator实现类中调用next()时,会“调用checkForComodification()比较‘expectedModCount’和‘modCount’的大小”;可是,CopyOnWriteArrayList的Iterator实现类中,没有所谓的checkForComodification(),更不会抛出ConcurrentModificationException异常!

  • 当添加元素的时候是进行加入了锁控制的

  • private volatile transient Object[] array;volatile修饰之后遍历集合的时候会实时获取最新的数组的长度

相关文章
相关标签/搜索