ConcurrentModificationException
Anonymous contributor
Anonymous contributor1 total contribution
Anonymous contributor
Published Apr 22, 2023
Contribute to Docs
The ConcurrentModificationException
exception is thrown when one thread is iterating through a collection using an Iterator
object, and another thread modifies the collection by adding, removing, or modifying its elements. When the Iterator
object tries to access the next element in the collection
, it detects that the collection has been modified, and throws the ConcurrentModificationException
.
Example
The following example throws the ConcurrentModificationException
because the arr
ArrayList is modified during iteration:
import java.util.ArrayList;public class ModificationError {public static void main(String args[]){// Creating an object of ArrayList ObjectArrayList<String> arr = new ArrayList<String>();arr.add("One");arr.add("Two");arr.add("Three");arr.add("Four");try {// Enhanced for loop, It uses the Iterator class under the hood.for (String elem : arr) {if (elem.equals("One")) {arr.remove(elem); // This will throw the exception.}System.out.println(elem); // Will not reach this line.}}catch (Exception e) {System.out.println(e);}}}
All contributors
- Anonymous contributorAnonymous contributor1 total contribution
- Anonymous contributor
Looking to contribute?
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.