问题描述

大量数据导出问题,使用了多线程导出的方式,由于业务需求,使用了subList方法对原有的List进行分割,导致报错

Unable to evalute the expression Method threw ‘java.until.ConcurrentMethod Exception’ exception

问题探究

因为导出之前对数据处理分为了两种方式,第一种方式处理数据后,多线程导出时并没有这个问题,而第二种方式导出导致了并发问题,觉得可能是subList的问题。
subList方法返回的是原list中某段数据的视图,然后ArrayList$SubList.size方法,如果原List和SubList的modCount不相等就会报错

public E set(int index, E e) {
    rangeCheck(index);
    checkForComodification();
    E oldValue = ArrayList.this.elementData(offset + index);
    ArrayList.this.elementData[offset + index] = e;
    return oldValue;
}
public E get(int index) {
    rangeCheck(index);
    checkForComodification();
    return ArrayList.this.elementData(offset + index);
}
 
private void checkForComodification() {
     if (ArrayList.this.modCount != this.modCount)
         throw new ConcurrentModificationException();
     }

解决方案

创建一个新的List对象进行包装

new ArrayList(list1.subList(0, list1.size()))

或者使用创建新的List,通过for循环添加。

目录

Total Likes
1
Total Comments
0