java.util.ConcurrentModificationException

java.util.ConcurrentModificationException is a very familiar exception when working with Java collection classes. Classes in the Java collection are fast, which means that iterator.next() will throw ConcurrentModificationException.

ConcurrentModificationExceptions can occur in a multi-threaded and single-threaded Java programming environment.

java.util.ConcurrentModificationException

Following is the ConcurrentModificationException scenario with an example.

package com.HandyOpinion.Concurrent_Modification_Exception;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class Concurrent_Modification_Exception_Example {

	public static void main(String args[]) {
		List<String> myList = new ArrayList<String>();

		myList.add("1");
		myList.add("2");
		myList.add("3");
		myList.add("4");
		myList.add("5");

		Iterator<String> it = myList.iterator();
		while (it.hasNext()) {
			String value = it.next();
			System.out.println("List Value:" + value);
			if (value.equals("3"))
				myList.remove(value);
		}

		Map<String, String> myMap = new HashMap<String, String>();
		myMap.put("1", "1");
		myMap.put("2", "2");
		myMap.put("3", "3");

		Iterator<String> it1 = myMap.keySet().iterator();
		while (it1.hasNext()) {
			String key = it1.next();
			System.out.println("Map Value:" + myMap.get(key));
			if (key.equals("2")) {
				myMap.put("1", "4");
				// myMap.put("4", "4");
			}
		}

	}
}

The above code will throw exception java.util.ConcurrentModificationException when the code executes as shown in the below console logs.

List Value:1
List Value:2
List Value:3
Exception in thread "main" java.util.ConcurrentModificationException
	at java.base/java.util.ArrayList$Itr.checkForComodification(ArrayList.java:937)
	at java.base/java.util.ArrayList$Itr.next(ArrayList.java:891)
	at com.HandyOpinion.ConcurrentModificationException.ConcurrentModificationExceptionExample.main(ConcurrentModificationExceptionExample.java:22)

From the output stack trace, it is clear that the simultaneous change exception is thrown when we call the iterator function next().

Implementation is in the AbstractList class, which has an int modCount variable define. The modCount indicates how often the size of the list has changed. The modCount value is used each time next() is called to look for changes in a checkForComodificatio() function.

Now comment out the part of the list and run the program again. You will see that no ConcurrentModificationException is thrown now.

Output

Map Value:3
Map Value:2
Map Value:4

Since we are updating the existing key-value in myMap, its size has not changed and we do not get a ConcurrentModificationException. The output may be different in your system because the HashMap key set is not in order as a list.

If you comment out the statement we were adding a new key-value in the HashMap, a ConcurrentModificationException is thrown.

Next Article

1. To Avoid ConcurrentModificationException in a multi thread environment
2. Loop to avoid java.util.ConcurrentModificationException

Please share this post:
Posts created 58

Ask a Question

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Related Posts

Begin typing your search term above and press enter to search. Press ESC to cancel.

%d bloggers like this: