快速失败机制

定义

通过迭代器遍历一个集合对象时,如果在遍历过程中对集合对象进行了修改,则在下一次迭代时会抛出ConcureentModificationException.

原理

每个集合对象都有一个modCount值,每一次对集合进行修改,modCount值都会加1。准备对过迭代器对集合进进行遍历时,首先会设置expectedModCount值等于当前modCount值,然后每一次迭代都会对expectedModCount与最新的modCount值进行比较,如果两者相同则继续,否则就会导出ConcurentModificationException.

触发modCount++

以ArrayList的源码为例,查看哪些操作会触发modCount++
1. 容量整理
下面展示一些相关的源代码。

public void trimToSize() {
        modCount++;
        if (size < elementData.length) {
            elementData = (size == 0)
              ? EMPTY_ELEMENTDATA
              : Arrays.copyOf(elementData, size);
        }
    }

2. 扩容

     private void ensureExplicitCapacity(int minCapacity) {
        modCount++;

        // overflow-conscious code
        if (minCapacity - elementData.length > 0)
            grow(minCapacity);
    }

3. 根据下标删除元素

 public E remove(int index) {
        rangeCheck(index);

        modCount++;
        E oldValue = elementData(index);

        int numMoved = size - index - 1;
        if (numMoved > 0)
            System.arraycopy(elementData, index+1, elementData, index,
                             numMoved);
        elementData[--size] = null; // clear to let GC do its work

        return oldValue;
    }

4. 根据元素对象删除

    public boolean remove(Object o) {
        if (o == null) {
            for (int index = 0; index < size; index++)
                if (elementData[index] == null) {
                    fastRemove(index);
                    return true;
                }
        } else {
            for (int index = 0; index < size; index++)
                if (o.equals(elementData[index])) {
                    fastRemove(index);
                    return true;
                }
        }
        return false;
    }

5. 清空集合

    public void clear() {
        modCount++;

        // clear to let GC do its work
        for (int i = 0; i < size; i++)
            elementData[i] = null;

        size = 0;
    }

6. 在未尾添加参数集合中的元素

    public boolean addAll(Collection<? extends E> c) {
        Object[] a = c.toArray();
        int numNew = a.length;
        ensureCapacityInternal(size + numNew);  // Increments modCount
        System.arraycopy(a, 0, elementData, size, numNew);
        size += numNew;
        return numNew != 0;
    }

7. 在指定的位置开始添加

   public boolean addAll(int index, Collection<? extends E> c) {
        rangeCheckForAdd(index);

        Object[] a = c.toArray();
        int numNew = a.length;
        ensureCapacityInternal(size + numNew);  // Increments modCount

        int numMoved = size - index;
        if (numMoved > 0)
            System.arraycopy(elementData, index, elementData, index + numNew,
                             numMoved);

        System.arraycopy(a, 0, elementData, index, numNew);
        size += numNew;
        return numNew != 0;
    }

8. 清除指定范围元素

    protected void removeRange(int fromIndex, int toIndex) {
        modCount++;
        int numMoved = size - toIndex;
        System.arraycopy(elementData, toIndex, elementData, fromIndex,
                         numMoved);

        // clear to let GC do its work
        int newSize = size - (toIndex-fromIndex);
        for (int i = newSize; i < size; i++) {
            elementData[i] = null;
        }
        size = newSize;
    }

9. 清除参数集合中包含的元素

 public boolean removeAll(Collection<?> c) {
        Objects.requireNonNull(c);
        return batchRemove(c, false);
    }

10. 清除不包含在参数集合中的元素

    public boolean retainAll(Collection<?> c) {
        Objects.requireNonNull(c);
        return batchRemove(c, true);
    }

11. 根据条件清除
基于方法太长,仅列方法的声明

 @Override
    public boolean removeIf(Predicate<? super E> filter)

12. 元素替换

    @Override
    @SuppressWarnings("unchecked")
    public void replaceAll(UnaryOperator<E> operator) {
        Objects.requireNonNull(operator);
        final int expectedModCount = modCount;
        final int size = this.size;
        for (int i=0; modCount == expectedModCount && i < size; i++) {
            elementData[i] = operator.apply((E) elementData[i]);
        }
        if (modCount != expectedModCount) {
            throw new ConcurrentModificationException();
        }
        modCount++;
    }

13. 元素排序

    @Override
    @SuppressWarnings("unchecked")
    public void sort(Comparator<? super E> c) {
        final int expectedModCount = modCount;
        Arrays.sort((E[]) elementData, 0, size, c);
        if (modCount != expectedModCount) {
            throw new ConcurrentModificationException();
        }
        modCount++;
    }

一边遍历一边修改

以上的方法都会修改modCount,而修改必须为触发modCount++,为了防止ConcurrentModificationException,有两个思路:
一、防止expectedModCount与modCount比较。
二、将expectedModCount更新为最新的modCount
两种方式的实现代码如下:
代码一:从前往后遍历,不比较

		List<String> list = new ArrayList<String>();
		list.add("aaa");
		list.add("bbb");
		list.add("ccc");
		
		//从前往后遍历,不比较
		for( int i = 0; i < list.size(); i ++ ){
			String temp = list.get( i );
			if( "bbb".equals( temp ) ){
				list.remove( i );
				i --;
			}
			else{
				System.out.println( temp );
			}
		}

代码二,从后往前遍历,不比较

		List<String> list = new ArrayList<String>();
		list.add("aaa");
		list.add("bbb");
		list.add("ccc");
		
		//从后往前遍历, 不比较
		for( int i = list.size() - 1 ; i >= 0; i -- ){
			String temp = list.get( i );
			if( "bbb".equals( temp ) ){
				list.remove( i );
			}
			else{
				System.out.println( temp );
			}
		}

代码三,重置迭代器(这种方式会导致一定的遍历重复,可以通过代码实现去重)

		List<String> list = new ArrayList<String>();
		list.add("aaa");
		list.add("bbb");
		list.add("ccc");
		
		//遍历
		Iterator<String> it = list.iterator();
		while( it.hasNext() ){
			String temp = it.next();
			if( "bbb".equals( temp ) ){
				list.remove( temp );
				//会导致前续元素重复遍历
				it = list.iterator();
			}
			else{
				System.out.println(temp);
			}
		}

安全失败机制

当需要对集合进行修改时,先copy一个集合的副本,对集合的副本进行修改,这样因为没有对原集合进行操作,所以不会触发原集合的modCount++,也就不会触发CocurrentModificationException.这种方式的缺点是:原集合在遍历过程中,无法获取到修改后的数据。
java.util.concurrent下的包都是安全失败的,可以并发的使用。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值