[Java] What is Concurrent Modification Exception?

Programming study diary

November 15, 2020 When I wrote the code in Java and checked if it was using the test, I got an exception called java.util.ConcurrentModificationException, so I will summarize what the ConcurrentModificationException is and what to do.

What is ConcurrentModificationException?

Javadoc for ConcurrentModificationException states as follows.

This exception is thrown when a method that detects a parallel change in an object does not allow such a change.

Basically, it is used to fail-first to fail early and throw an exception when something that repeats changes </ b>. That is, an exception has occurred before the iterative process is completed.

The for-each statement uses Iterator, which isn't very verbose, but you can access additional methods such as remove () if you refactor your test to use Iterator. The remove method does not cause a ConcurrentModificationException, so it is okay to call it during an iterative process.

In my code, I had a problem with how to handle ArrayList. ArrayList and Iterator internally hold the "number of changes". The Iterator generated by the Iterator () method of ArrayList is checking whether there is a deviation in the number of changes when starting the next () method etc. used when turning with for, and because there is a deviation, an exception of ConcurrentModificationException Was occurring. There was the following description in Javadoc ArrayList.

The iterator returned by the iterator and listIterator methods of this class is failfast. After creating an iterator, the iterator throws a ConcurrentModificationException if the list is structurally modified by any method other than the remove or add method of the iterator itself. In this way, when changes are made in parallel, the iterator throws an exception immediately and gracefully to avoid the risk of unpredictable behavior at unpredictable points in the future. Iterator failfast behavior cannot be guaranteed, as there is usually no reliable guarantee when there are asynchronous parallel changes. The failfast iterator throws a ConcurrentModificationException based on the best effort principle. Therefore, it is a mistake to write a program that depends on this exception for accuracy. "Iterator failfast behavior should only be used to detect bugs."

References

Class ConcurrentModificationException Avoid ConcurrentModificationException in Java

Recommended Posts

[Java] What is Concurrent Modification Exception?
What is java
What is Java <>?
What is Java Encapsulation?
What is Java technology?
What is Java API-java
[Java] What is flatMap?
[Java] What is JavaBeans?
[Java] What is ArrayList?
What is Java Assertion? Summary.
What is a Java collection?
[Java] What is jaee j2ee?
[Java] What is class inheritance?
[Java basics] What is Class?
What is java escape analysis?
What is JVM (Java Virtual Machine)?
What is thread safe (with Java)
What is a lambda expression (Java)
What is a class in Java language (3 /?)
What is the best file reading (Java)
What is a class in Java language (1 /?)
What is Java and Development Environment (MAC)
What is a class in Java language (2 /?)
What is the main method in Java?
Java Exception StackTrace is determined when new
Java exception handling?
What is Docker?
What is null? ]
What is Keycloak
What is Jackson?
What is Docker
What is self
What is Jenkins
What is ArgumentMatcher?
What is IM-Juggling?
What is params
What is SLF4J?
What is Facade? ??
What is Gradle?
What is centOS
[Java] Exception handling
What is RubyGem?
What is programming?
☾ Java / Exception handling
What is before_action?
What is Docker
Java exception handling
Java exception handling
What is Byte?
What is Tomcat
What is the Java Servlet / JSP MVC model?
What is the volatile modifier for Java variables?
What is `docker-compose up`?
What is a constructor?
[java] throw an exception
What is hard coding?
What is a stream
What is permission denied?
What is instance control?
What is Spring Tools 4
What is an operator?