集合的 modCount

14 篇文章 0 订阅
10 篇文章 0 订阅

AbstractList
    迭代器认为集合应该返回的修改值,如果和期望的值不一致,那么迭代器检测到了并发修改
 protected transient int modCount = 0;

 List迭代时定义了期望的修改值:
    int expectedModCount = modCount;

判定迭代是否结束条件:
   迭代器的 next()方法在调用时会检测期望值与集合修改值是否一致

AbstractList<E>

定义了一个modCount属性。

       protected transient int modCount = 0; 

解释:

         这个字段被用于  iterator 和  list  iterator , 如果这个值被意外的修改, 这个迭代器将会抛出一个 

ConcurrentModificationException异常,这提供了 失败快速 行为,在迭代期间面对并发修改。

适应范围: 

           {@code next},  {@code remove}, {@code previous},

           {@code set} or {@code add}

​​​​​​ArrayList<E>

add(E e)方法, 对 modCount字段维护,每次新增元素 + 1

 public boolean add(E e) {
        ensureCapacityInternal(size + 1);  // Increments modCount!!
        elementData[size++] = e;
        return true;
    }

    private static int calculateCapacity(Object[] elementData, int minCapacity) {
        if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) {
            return Math.max(DEFAULT_CAPACITY, minCapacity);
        }
        return minCapacity;
    }

    private void ensureCapacityInternal(int minCapacity) {
        ensureExplicitCapacity(calculateCapacity(elementData, minCapacity));
    }

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

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

remove( ) 移除:

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

迭代器迭代过程中对 modCount进行检查 

​
 private class Itr implements Iterator<E> {
        int cursor;       // index of next element to return
        int lastRet = -1; // index of last element returned; -1 if no such
        int expectedModCount = modCount;

        Itr() {}
        
        // 判断是否退出迭代的条件
        public boolean hasNext() {
            return cursor != size;
        }

        
        @SuppressWarnings("unchecked")
        public E next() {
            // 检查modCount
            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() {
            // 集合的modCount与期望值是否一致
            if (modCount != expectedModCount)
                throw new ConcurrentModificationException();
        }

​

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值