美文网首页
集合中的modCount

集合中的modCount

作者: 雨中独奏 | 来源:发表于2019-01-05 12:07 被阅读0次

List,map这些集合结构中常能看到modCount属性,在对集合进行新增或移除操作时会使modCount+1,这是一个记录集合变化次数的变量。作用是在使用迭代器Iterator对集合进行遍历时,用modCount来判断集合内数据没有发生变化,否则会抛出异常。

ArrayList中的Iterator:

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;

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

1、expectedModCount的初值为modCount
2、hasNext的判断条件为cursor!=size,就是当前迭代的位置不是数组的最大容量值就返回true
3、next和remove操作之前都会先调用checkForComodification来检查expectedModCount和modCount是否相等

如果没checkForComodification去检查expectedModCount与modCount相等,这个程序肯定会报越界异常ArrayIndexOutOfBoundsException

因为有modCount的存在,在使用多线程对非线程安全的集合进行操作时,使用迭代器循环会产生modCount != expectedModCount的情况,会抛出异常。

再查看Vector这个线程安全的list,它的内部实现,在新增和移除方法上添加了sychorized关键字,并且内部迭代器的next()方法也进行了线程同步:

public E next() {
            synchronized (Vector.this) {
                checkForComodification();
                int i = cursor;
                if (i >= elementCount)
                    throw new NoSuchElementException();
                cursor = i + 1;
                return elementData(lastRet = i);
            }
        }

相关文章

  • 集合中的modCount

    List,map这些集合结构中常能看到modCount属性,在对集合进行新增或移除操作时会使modCount+1,...

  • 为什么需要modCount?

    Java集合类中常见的变量 -- modCount 在java的集合类中常见的变量modCount,用于记录对象的...

  • 集合类中的modCount字段

    在ArrayList, LinkedList, HashMap集合类中会发现modCount这个字段应用在很多方法...

  • Java 集合

    fail-fast机制 在java集合类中,使用modCount来检查数组的状态.当在迭代集合的时候,(通常会实现...

  • Java集合中的fail-fast机制

    fail-fast简介 在java.util包下的集合类中,有一个成员变量modCount。 这个变量的数值表示使...

  • fail-fast 与 fail-safe 是什么?

    fail-fast 快速失败,是指在遍历集合时,检查遍历过程中集合的modCount是否改变,如果改变就会抛出ja...

  • JAVA的数据结构

    Iterator:迭代器,维护一个modcount,继承该接口的所以类都将自身的modcount传给迭代器,用于多...

  • 抛ConcurrentModificationException

    final void checkForComodification() { if(modCount!=expect...

  • 集合的modCount字段和Fail-Fast,Fail-Saf

    发现前面博客遗失了一篇ArrayList源码解析的文章,这里就不打算重写了,但是其中关于Fail-Fast机制的知...

  • ModCount属性意义

    上面的代码是在ArrayList、HashMap、LinkedList中找到的,属性名一样,定义方式类似。tran...

网友评论

      本文标题:集合中的modCount

      本文链接:https://www.haomeiwen.com/subject/pxlrrqtx.html