search
HomeJavajavaTutorialIn what scenarios does ConcurrentModificationException occur in Java?

Java中的ConcurrentModificationException异常在什么场景下出现?

ConcurrentModificationException(并发修改异常)是Java中常见的异常之一,当在迭代(遍历)集合时,存在另外一个线程修改了集合中的元素,就会抛出该异常。

ConcurrentModificationException 异常在什么场景下出现?

在多线程环境下,当一个线程在迭代一个集合的过程中,另外一个线程在修改了该集合后就会抛出ConcurrentModificationException异常。简单来说,就是当一个线程正在对集合进行迭代操作,而另一个线程此时对集合进行了添加、删除或修改操作,就会触发该异常。这通常发生在使用Iterator迭代器对集合操作时,因为Iterator只允许单线程对集合进行操作。

例如,在以下例子中,我们使用一个线程对集合进行迭代,在同一时间内另一个线程对集合进行了修改,就会引发ConcurrentModificationException异常:

import java.util.ArrayList;
import java.util.List;
  
public class ConcurrencyTest {
    public static void main(String[] args) {
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            list.add(i);
        }
 
        new Thread(() -> {
            for (int i : list) {
                System.out.println(i);
                try {
                    Thread.sleep(50);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
 
        new Thread(() -> {
            list.add(100);
        }).start();
    }
}

如上代码所示,我们启动两个线程。其中一个线程对集合进行迭代操作,另一个线程则对集合进行添加操作。这里我们使用ArrayList作为集合。

如果运行以上代码,将会看到以下异常抛出:

Exception in thread "Thread-1" java.util.ConcurrentModificationException
    at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:901)
    at java.util.ArrayList$Itr.next(ArrayList.java:851)
    at ConcurrencyTest.lambda$0(ConcurrencyTest.java:16)

如何解决ConcurrentModificationException异常?

解决ConcurrentModificationException异常的最好方法就是避免此类场景的出现。为了保证线程安全,我们必须通过使用同步操作或并发集合来避免多线程冲突。

Java中的并发集合类如:ConcurrentHashMap,ConcurrentLinkedQueue,CopyOnWriteArrayList等,在多线程程序中,这些集合类可以大大减少多线程冲突的发生。

对于普通的集合类,比如ArrayList、LinkedList、HashMap等,我们可以使用synchronized关键字来保证线程安全。比如,使用synchronized同步代码块来保证在迭代时不被其他线程操作:

import java.util.ArrayList;
import java.util.List;
  
public class ConcurrencyTest {
    public static void main(String[] args) {
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            list.add(i);
        }
 
        new Thread(() -> {
            synchronized (list) {
                for (int i : list) {
                    System.out.println(i);
                    try {
                        Thread.sleep(50);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
 
        new Thread(() -> {
            synchronized (list) {
                list.add(100);
            }
        }).start();
    }
}

如上代码所示,我们使用synchronized关键字对迭代和添加进行同步,从而避免了多线程冲突的问题。

总结

ConcurrentModificationException异常在Java中非常常见,尤其是在多线程操作集合的时候。为了避免出现这种异常,我们可以使用并发集合和同步关键字来确保线程安全。在实际开发中,我们必须针对不同场景选择合适的解决方案,以保证程序的健壮性。

The above is the detailed content of In what scenarios does ConcurrentModificationException occur in Java?. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log?How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log?Apr 19, 2025 pm 11:45 PM

Start Spring using IntelliJIDEAUltimate version...

How to elegantly obtain entity class variable names to build database query conditions?How to elegantly obtain entity class variable names to build database query conditions?Apr 19, 2025 pm 11:42 PM

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

How to use the Redis cache solution to efficiently realize the requirements of product ranking list?How to use the Redis cache solution to efficiently realize the requirements of product ranking list?Apr 19, 2025 pm 11:36 PM

How does the Redis caching solution realize the requirements of product ranking list? During the development process, we often need to deal with the requirements of rankings, such as displaying a...

How to safely convert Java objects to arrays?How to safely convert Java objects to arrays?Apr 19, 2025 pm 11:33 PM

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

How do I convert names to numbers to implement sorting and maintain consistency in groups?How do I convert names to numbers to implement sorting and maintain consistency in groups?Apr 19, 2025 pm 11:30 PM

Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products?E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products?Apr 19, 2025 pm 11:27 PM

Detailed explanation of the design of SKU and SPU tables on e-commerce platforms This article will discuss the database design issues of SKU and SPU in e-commerce platforms, especially how to deal with user-defined sales...

How to set the default run configuration list of SpringBoot projects in Idea for team members to share?How to set the default run configuration list of SpringBoot projects in Idea for team members to share?Apr 19, 2025 pm 11:24 PM

How to set the SpringBoot project default run configuration list in Idea using IntelliJ...

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software