博客
关于我
并发编程进阶03-java.util.ConcurrentModificationException异常-多线程情况
阅读量:115 次
发布时间:2019-02-26

本文共 5680 字,大约阅读时间需要 18 分钟。

多线程环境下ConcurrentModificationException异常及解决方案

在多线程环境下,Java的集合类如ArrayList和Vector在进行迭代操作时,如果在迭代过程中集合被修改,可能会抛出ConcurrentModificationException异常。这一问题源于集合迭代器在设计时未考虑并发修改的可能性,导致在某些并发场景下出现意外行为。以下将详细分析这一问题,并探讨解决方案。


问题再现

测试代码

public class Test3 {    public static void main(String[] args) {        final ArrayList
arrayList = new ArrayList<>(); for (int i = 0; i < 20; i++) { arrayList.add(Integer.valueOf(i)); } Thread thread1 = new Thread(new Runnable() { public void run() { Iterator
iterator = arrayList.iterator(); while (iterator.hasNext()) { System.out.println("Thread1 " + iterator.next().intValue()); } try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } } }); Thread thread2 = new Thread(new Runnable() { public void run() { Iterator
iterator = arrayList.iterator(); while (iterator.hasNext()) { System.out.println("thread2 " + iterator.next().intValue()); iterator.remove(); } } }); thread1.start(); thread2.start(); }}

输出结果

Thread1 0thread2 1thread2 2thread2 3thread2 4thread2 5thread2 6thread2 7thread2 8thread2 9thread2 10thread2 11thread2 12thread2 13thread2 14thread2 15thread2 16thread2 17thread2 18thread2 19java.util.ConcurrentModificationException    at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:901)    at java.util.ArrayList$Itr.next(ArrayList.java:851)    at com.bruceliu.demo17.Test3$1.run(Test3.java:26)    at java.lang.Thread.run(Thread.java:745)

问题分析

在上述测试代码中,两个线程同时操作同一个ArrayList:

  • Thread1负责读取并输出集合中的元素。
  • Thread2负责读取并修改集合中的元素。

由于ArrayList的迭代器在遍历时会维护一个expectedModCount字段,用于检测集合是否被修改。在本例中:

  • Thread2修改了集合,导致modCount从20增加到21。
  • Thread1的迭代器的expectedModCount仍为20,未感知到集合的修改。

Thread1试图访问集合时,发现modCount与迭代器的expectedModCount不符,从而抛出ConcurrentModificationException异常。


解决方案

为了解决ConcurrentModificationException问题,可以采取以下两种方案:

方案一:使用同步锁

在多线程环境下,对集合进行加锁操作,可以确保在同一时间内只有一个线程对集合进行操作。具体实现如下:

public class Test3 {    public static void main(String[] args) {        final ArrayList
arrayList = new ArrayList<>(); for (int i = 0; i < 20; i++) { arrayList.add(Integer.valueOf(i)); } Thread thread1 = new Thread(new Runnable() { public void run() { synchronized (arrayList) { Iterator
iterator = arrayList.iterator(); while (iterator.hasNext()) { System.out.println("Thread1 " + iterator.next().intValue()); } try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } } } }); Thread thread2 = new Thread(new Runnable() { public void run() { synchronized (arrayList) { Iterator
iterator = arrayList.iterator(); while (iterator.hasNext()) { System.out.println("thread2 " + iterator.next().intValue()); iterator.remove(); } } } }); thread1.start(); thread2.start(); }}

优点:

  • 通过加锁确保线程安全,避免了ConcurrentModificationException问题。
  • 适用于所有需要多线程操作集合的场景。缺点:
  • 同时锁定整个集合,可能导致并发性能下降。

方案二:使用CopyOnWriteArrayList

CopyOnWriteArrayList是一种线程安全的集合类,它通过在每次修改时创建新的数组副本来避免并发修改问题。具体实现如下:

public class Test3 {    public static void main(String[] args) {        final CopyOnWriteArrayList
list = new CopyOnWriteArrayList<>(); for (int i = 0; i < 20; i++) { list.add(Integer.valueOf(i)); } Thread thread1 = new Thread(new Runnable() { public void run() { for (Integer integer : list) { System.out.println("thread1 " + integer.intValue()); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } }); Thread thread2 = new Thread(new Runnable() { public void run() { for (Integer integer : list) { System.out.println("thread2 " + integer.intValue()); if (integer.intValue() == 5) { list.remove(integer); } } for (Integer integer : list) { System.out.println("thread2 again " + integer.intValue()); } } }); thread1.start(); thread2.start(); }}

优点:

  • 支持在多线程环境下安全地进行集合操作。
  • 避免了ConcurrentModificationException问题。缺点:
  • 每次修改都需要创建新的数组副本,可能影响性能。
  • CopyOnWriteArrayList的迭代器不支持remove()操作,需手动实现。

实现细节分析

CopyOnWriteArrayList的工作原理

CopyOnWriteArrayList通过维护一个Object[] array数组,所有修改操作都基于这个数组的副本。具体实现如下:

  • getArray()方法返回当前数组的副本。
  • 修改操作(如addremoveclear)会创建新的数组副本,并对新数组进行操作。
  • 遍历操作(如forEachiterator())基于当前数组的副本完成,确保遍历过程中不受修改影响。
  • CopyOnWriteArrayList的优缺点

    优点:

    • 线程安全,避免ConcurrentModificationException。
    • 支持一边遍历一边修改。

    缺点:

    • 修改操作需要创建新的数组,可能影响性能。
    • 迭代器不支持remove()操作,需要手动实现。

    测试结果分析

    在测试代码中,thread2对集合进行了多次修改,但thread1未感知到这些修改。通过使用CopyOnWriteArrayList,thread1thread2能够安全地进行并发操作,而不会抛出ConcurrentModificationException。


    总结

    在多线程环境下,ConcurrentModificationException问题的主要原因是集合迭代器未能检测到集合的修改操作。通过使用同步锁或CopyOnWriteArrayList,可以有效避免这一问题。选择哪种方案取决于具体的性能需求和场景限制。

    转载地址:http://gndu.baihongyu.com/

    你可能感兴趣的文章
    no session found for current thread
    查看>>
    No toolchains found in the NDK toolchains folder for ABI with prefix: mips64el-linux-android
    查看>>
    NO.23 ZenTaoPHP目录结构
    查看>>
    no1
    查看>>
    NO32 网络层次及OSI7层模型--TCP三次握手四次断开--子网划分
    查看>>
    NoClassDefFoundError: org/springframework/boot/context/properties/ConfigurationBeanFactoryMetadata
    查看>>
    Node JS: < 一> 初识Node JS
    查看>>
    Node Sass does not yet support your current environment: Windows 64-bit with Unsupported runtime(72)
    查看>>
    Node-RED中使用JSON数据建立web网站
    查看>>
    Node-RED中使用json节点解析JSON数据
    查看>>
    Node-RED中使用node-random节点来实现随机数在折线图中显示
    查看>>
    Node-RED中使用node-red-browser-utils节点实现选择Windows操作系统中的文件并实现图片预览
    查看>>
    Node-RED中使用node-red-contrib-image-output节点实现图片预览
    查看>>
    Node-RED中使用node-red-node-ui-iframe节点实现内嵌iframe访问其他网站的效果
    查看>>
    Node-RED中使用Notification元件显示警告讯息框(温度过高提示)
    查看>>
    Node-RED中实现HTML表单提交和获取提交的内容
    查看>>
    Node-RED中建立Websocket客户端连接
    查看>>
    Node-RED中通过node-red-ui-webcam节点实现访问摄像头并截取照片预览
    查看>>
    node-request模块
    查看>>
    Node.js 8 中的 util.promisify的详解
    查看>>