话不多说,先看AbstractList的内部类Itr中的next()方法,在此之前了解下Itr类,该类实现了Iterator接口。
private class Itr implements Iterator {
/**
* Index of element to be returned by subsequent call to next.
*/
int cursor = 0;
/**
* Index of element returned by most recent call to next or
* previous. Reset to -1 if this element is deleted by a call
* to remove.
*/
int lastRet = -1;
/**
* The modCount value that the iterator believes that the backing
* List should have. If this expectation is violated, the iterator
* has detected concurrent modification.
*/
int expectedModCount = modCount;
public boolean hasNext() {
return cursor != size();
}
public E next() {
checkForComodification();
try {
int i = cursor;
E next = get(i);
lastRet = i;
cursor = i + 1;
return next;
} catch (IndexOutOfBoundsException e) {
checkForComodification();
throw new NoSuchElementException();
}
}
public void remove() {
if (lastRet < 0)
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方法
public E next() {
checkForComodification();
try {
int i = cursor;
E next = get(i);
lastRet = i;
cursor = i + 1;
return next;
} catch (IndexOutOfBoundsException e) {
checkForComodification();
throw new NoSuchElementException();
}
}
在执行next时,首先执行了checkForComodification()方法,其代码如下:
final void checkForComodification() {
if (modCount != expectedModCount)
throw new ConcurrentModificationException();
}
这里需要说明下,modCount时AbstractList的私有变量,表示该list数据在结构上被修改的次数。结构上修改地含义是指改变list的大小,如add、remove、addAll等方法。
同时,也可以看到expectedModCount是Itr的私有变量,初始化的值为modCount,如下:
int expectedModCount = modCount;
也就是说如果在进行next遍历list的时候,如果modCount的值发生了改变,那么就会抛出ConcurrentModificationExcption的错误。根据这个错误的名称,我们也可以推测,这是一个多线程并发引起的一个错误。例如,一个线程在遍历一个list的过程中,另一个线程对list在“结构上”进行了改变,例如add和remove操作。
为了进一步验证,add或remove方法是不是在执行的时候改变了modCount的值,我找到了实现类ArrayList查看了它的add源码如下:
public boolean add(E e) {
ensureCapacityInternal(size + 1); // Increments modCount!!
elementData[size++] = e;
return true;
}
接着往下看到ensureCapacityInternal方法:
private void ensureCapacityInternal(int minCapacity) {
if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) {
minCapacity = Math.max(DEFAULT_CAPACITY, minCapacity);
}
ensureExplicitCapacity(minCapacity);
}
在接着看到ensureExplicitCapacity方法:
private void ensureExplicitCapacity(int minCapacity) {
modCount++;
// overflow-conscious code
if (minCapacity - elementData.length > 0)
grow(minCapacity);
}
至此,终于印证了之前的想法,在这里modCount++执行了自加操作,表示add方法对list在“结构上”改变的次数是1。
因此,如果有两个线程在同时执行,第一个线程遍历,第二个add操作,就会导致上面的ConcurrentModificationExcption错误。