What is Concurrent Modification Exception in Java?

By | August 9, 2025

The ConcurrentModificationException is a runtime exception in Java, which means the Java compiler does not require you to handle it explicitly using try-catch blocks. The Runtime exception is thrown automatically at runtime when it occurs.

ConcurrentModificationException typically happens when a Java collection — such as a List, Set, or Map — is modified while being iterated, either using an iterator or an enhanced for-each loop, without using the iterator’s remove() method. It is thrown mainly by fail-fast iterators of Java collections like ArrayList, HashMap, HashSet etc.

What is fail-fast iterator in Java?

A fail-fast iterator in Java is a type of iterator that immediately throws a ConcurrentModificationException if it detects that the underlying collection has been structurally modified after the iterator was created, except when the modification is done through the iterator’s own remove() method.

Fail-fast iterators are commonly used in Java collections like ArrayList, HashMap, and HashSet. They work by tracking a modification counter (modCount) in the collection. When an iterator is created, it stores the current value in expectedModCount. If any structural modification (add, remove, clear, etc.) occurs outside of the iterator, modCount changes, and the iterator detects this mismatch, triggering a ConcurrentModificationException.

Key Points about Fail-Fast Iterators in Java:

  • Designed for data consistency and error prevention.
  • Detects concurrent modifications in real-time.
  • Throws ConcurrentModificationException if modified outside iterator.
  • Common in non-thread-safe collections in java.util package.

Java ConcurrentModificationException Example

Java
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * ConcurrentModificationException Example
 * 
 * @author ankur.pal
 */
public class Main {
	
	public static void main(String[] args) {
		
		//List of names
		List<String> names = new ArrayList<String>();
		names.add("Shruti");
		names.add("Ankur");
		names.add("Abhinav");
		names.add("Sudhanshu");
		names.add("Utkarsh");
		
		//Iterator
		Iterator<String> itr = names.iterator();
		while(itr.hasNext()) {
			String name = itr.next();
			//If the name is "Shruti", you are trying to remove it from the list
			//that means changing the structure of the list
			if(name.equals("Shruti")) {
				names.remove(name);
			}
			System.out.println(name);
		}
	}
}

Output:

What is Concurrent Modification Exception in Java?

In the above Java ConcurrentModificationException example, we declared a list of names and created an Iterator to traverse it.

When the iterator is created, Java stores the current modification count of the collection in a variable called expectedModCount (often managed internally using modCount). This count represents the number of structural changes made to the collection.

While iterating, if we try to modify the collection directly (such as using names.remove(name) instead of iterator.remove()), the modCount in the collection will change. The iterator then compares its stored expectedModCount with the current modCount.

If these two values do not match, the iterator immediately throws a ConcurrentModificationException at runtime. This is known as fail-fast behavior in Java collections, which is designed to prevent unpredictable results and ensure data consistency.

ConcurrentModificationException in Single-Threaded and Multi-Threaded Environments

A ConcurrentModificationException in Java can occur in both single-threaded and multi-threaded environments.

  • In a single-threaded environment, it happens when a collection is structurally modified (e.g., add, remove, clear) while iterating over it using a fail-fast iterator like those in ArrayList, HashMap, or HashSet. This usually occurs when the modification is done outside of the iterator’s own remove() method, causing a mismatch between the iterator’s expectedModCount and the collection’s modCount.
  • In a multi-threaded environment, it can occur when one thread is iterating over the collection while another thread modifies it at the same time, without proper synchronization or the use of concurrent collection classes. This leads to the same modCount mismatch, triggering the exception.

ConcurrentModificationException in multithreading example

Java
import java.util.ArrayList;
import java.util.List;

/**
 * ConcurrentModificationException in Multi-threading Example
 * 
 * @author ankur.pal
 */
public class Main {
	
	public static void main(String[] args) {
		
		//List of names
		List<String> names = new ArrayList<String>();
		names.add("Shruti");
		names.add("Ankur");
		names.add("Abhinav");
		names.add("Sudhanshu");
		names.add("Utkarsh");
		
		Thread t1 = new Thread() {
			
			@Override
			public void run() {
				for(String str : names) {
					System.out.println(str);
				}
			}
		};
		
		Thread t2 = new Thread() {
			
			@Override
			public void run() {
				names.remove(2);
			}
		};
		
		t1.start();
		t2.start();
	}
}

Output:

ConcurrentModificationException in multithreading example
Share this article

Leave a Reply

Your email address will not be published. Required fields are marked *