List的subList导致ConcurrentModificationException引发的思考
文章目录
一、案例如下
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);
}
}
2. 输出
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)
二、分析过程
1. 根据输出分析
- 从输出第一行可以看出,调用
#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(); }如果你使用debug,则会发现
this.modCount = 4而l.modCount = 5,所以会抛出异常。
-
那么上面的 this.modCount 和 l.modCount到底是什么呢?请继续往下看。。。
2. 追根溯源
-
需要从头开始说起了。
-
我们之前调用的
#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则没有实现这个接口。 -
AbstractList.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); } }从上面的代码可以看出
AbstractList.RandomAccessSubList是AbstractList.SubList的子类,所以归根结底调用的都是AbstractList.SubList类的构造方法。所以我们需要看一下该类的实现。 -
AbstractList.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分别代表的是子链表的修改次数和原始链表的修改次数。
最后,希望你们可以去看一下AbstractList.SubList类中的其他方法,所有的方法在执行之前都调用了checkForComodification 方法。所以不要对子链表执行AbstractList.SubList类中重写的方法。
三、总结
根据最后的分析,我们可以得到结论,当子链表的修改次数和原始链表的修改次数不一致时,对子链表执行任意包含checkForComodification 检查的操作,就会抛出ConcurrentModificationException。所以,我们在执行#subList方法之后,可以对原始链表进行操作,但是最好不要对子链表进行操作。
四、扩展
从AbstractList.SubList类的构造方法可以看出,子链表其实就是原始链表的一个视图(view),所有对子链表进行的操作都会映射到原始链表上。所以jdk的编写者为了安全,禁止对子链表进行操作。
其次,考虑到迭代器也会对链表进行修改(其实迭代器也是原始链表的一个视图),所以如果仔细看AbstractList中的代码,会发现所有AbstractList.Iterator的子类中重写的方法都包含checkForComodification 检查,所以如果要对迭代器进行数据操作(#toString隐式调用了Iterator),也要注意。

评论
登录后参与技术讨论,分享你的经验与见解。
登录 / 注册暂无评论