avatar

Exception in thread "main" java.util.ConcurrentModificationException

代码展示

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public static void main(String[] args) {

List<String> arrayList = new ArrayList<>();

arrayList.add("1");
arrayList.add("2");
arrayList.add("3");
arrayList.add("4");

for (String s1 : arrayList) {
if ("2".equals(s1)) arrayList.remove("2");
}

arrayList.forEach(s -> {
if ("2".equals(s)) arrayList.remove("2");
});

System.out.println(arrayList);

}

异常信息

1
2
3
4
Exception in thread "main" java.util.ConcurrentModificationException
at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:909)
at java.util.ArrayList$Itr.next(ArrayList.java:859)
at com.itpm.project.controller.AcceptApplyController.main(AcceptApplyController.java:41)

异常原因

图片描述

expectedModCount的值是循环刚开始时modCount的值所赋予的

modCount赋值expectedModCount

使用ArrayList提供的remove方法,在判断完值不为null后会调用fastRemove方法

remove方法调用fastRemove方法

在fastRemove方法中会首先将modCount进行自增,此时modCount的值就与expectedModCount的值不同了

modCount自增

加强for或foreach会在最后调用next方法,next方法首先调用了checkForComodification方法

next方法调用checkForComodification方法

checkForComodification方法中对modCount与expectedModCount进行对比,结果为true,从而抛出了以上异常

在这里插入图片描述

文字描述

1
加强for和foreach是通过迭代器Iterator来访问的,ArrayList的remove方法会调用fastRemove方法,在fastRemove方法中modCount会进行自增,每次加强for和foreach都会调用checkForComodification方法,checkForComodification方法会将modCount与expectedModCount进行比对,而modCount在ArrayList的remove方法在被调用时进行了自增,自然就与expectedModCount不一致,从而抛出了以上异常

解决方法

图片描述

在removeIf方法和remove方法,会重置expectedModCount,以保证多个迭代器同时遍历时的数据一致

removeIf
remove

代码展示

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public static void main(String[] args) {

List<String> arrayList = new ArrayList<>();

arrayList.add("1");
arrayList.add("2");
arrayList.add("3");
arrayList.add("4");

arrayList.removeIf("2"::equals);

Iterator<String> iterator = arrayList.iterator();

while (iterator.hasNext()) {
if ("2".equals(iterator.next())) iterator.remove();
}

System.out.println(arrayList);

}
文章作者: 123
文章链接: https://gao5805123.github.io/123/2021/05/07/Exception%20in%20thread%20main%20java.util.ConcurrentModificationException/
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 123
打赏
  • 微信
    微信
  • 支付宝
    支付宝