一、案例如下
1. 代码
package test;
import java.util.LinkedList;
import java.util.List;
public class Main {
public static void main(String[] args) {
List<Long> list = new LinkedList<>();
list.add(1L);
list.add(2L);
list.add(3L);
list.add(4L);
List<Long> list1 = list.subList(0, 2);
System.out.println(list1.getClass());
list.add(5L);
list1.clear();
System.out.println(list);
}
}
1. 输出
class java.util.SubList
Exception in thread "main" java.util.ConcurrentModificationException
at java.util.SubList.checkForComodification(AbstractList.java:769)
at java.util.SubList.size(AbstractList.java:645)
at java.util.AbstractList.clear(AbstractList.java:234)
at test.Main.main(Main.java:16)
二、分析过程
- 从输出第一行可以看出,调用
#subList方法返回的是AbstractList.SubList类的实例。 - 其次,根据异常信息进行分析。
-
需要进入
AbstractList.java:234看代码。具体如下:public void clear() { removeRange(0, size()); } -
然后,进入
size方法,也就是AbstractList.java:645。具体如下:public int size() { checkForComodification(); return size; } -
然后,进入
checkForComodification方法,也就是AbstractList.java:769。具体如下:private void checkForComodification() { if (this.modCount != l.modCount) throw new ConcurrentModificationException(); }
this.modCount = 4而l.modCount = 5,所以会抛出异常。 -
- 那么上面的
this.modCount和l.modCount到底是什么呢?这就需要从头开始说起了。
我们之前调用的#subList方法其实是AbstractList#subList 。具体如下:
public List<E> subList(int fromIndex, int toIndex) {
return (this instanceof RandomAccess ?
new RandomAccessSubList<>(this, fromIndex, toIndex) :
new SubList<>(this, fromIndex, toIndex));
}
RandomAccess代表可以实现随机访问(性能比较高),即底层使用数组实现的(例如ArrayList),而LinkedList则没有实现这个接口。
首先看一下Abstract.RandomAccessSubList类。具体如下:
class RandomAccessSubList<E> extends SubList<E> implements RandomAccess {
RandomAccessSubList(AbstractList<E> list, int fromIndex, int toIndex) {
super(list, fromIndex, toIndex);
}
public List<E> subList(int fromIndex, int toIndex) {
return new RandomAccessSubList<>(this, fromIndex, toIndex);
}
}
从上面的代码可以看出Abstract.RandomAccessSubList是Abstract.SubList的子类,所以归根结底调用的都是Abstract.SubList类的构造方法。那么,我们再看Abstract.SubList类。
class SubList<E> extends AbstractList<E> {
private final AbstractList<E> l;
private final int offset;
private int size;
SubList(AbstractList<E> list, int fromIndex, int toIndex) {
if (fromIndex < 0)
throw new IndexOutOfBoundsException("fromIndex = " + fromIndex);
if (toIndex > list.size())
throw new IndexOutOfBoundsException("toIndex = " + toIndex);
if (fromIndex > toIndex)
throw new IllegalArgumentException("fromIndex(" + fromIndex +
") > toIndex(" + toIndex + ")");
l = list;
offset = fromIndex;
size = toIndex - fromIndex;
this.modCount = l.modCount;
}
// ...
}
从上面的代码可以看出,l是旧的list,而 modCount代表的则是链表的修改次数。所以我们可以解读之前留下来的疑问。
this.modCount和l.modCount分别代表的是子链表的修改次数和原始链表的修改次数。
最后,希望你们可以去看一下Abstract.SubList类中的其他方法,所有的方法在执行之前都调用了checkForComodification方法。所以不要对子链表执行Abstract.SubList类中重写的方法。
三、总结
根据最后的分析,我们可以得到结论,当子链表的修改次数和原始链表的修改次数不一致时,对子链表执行任意包含checkForComodification检查的操作,就会抛出ConcurrentModificationException。所以,我们在执行#subList方法之后,可以对原始链表进行操作,但是最好不要对子链表进行操作。
四、扩展
从SubList类的构造方法可以看出,子链表其实就是原始链表的一个视图(view),所有对子链表进行的操作都会映射到原始链表上。所以jdk的编写者为了安全,禁止对子链表进行操作。
其次,考虑到迭代器也会对链表进行修改(其实迭代器也是原始链表的一个视图),所以如果仔细看AbstractList中的代码,会发现所有Iterator的子类中重写的方法都包含checkForComodification检查,所以如果要对迭代器进行数据操作(#toString隐式调用了Iterator),也要注意。