Thursday 24 June 2021

Understanding ConcurrentModificationException in Java

ConcurrentModificationException is thrown by a method when it detected a concurrent modification of an object when such modification is not permissible.

 

Let’s see it with an example.

for(Integer i : list) {
	System.out.println(i);
	list.remove(i);
}

 

In the above example, we are trying to remove the element while iterating. This is not permissible, if you want to remove the element while iterating, you can use iterator remove method.

 

ConcurrentModificationDemo1.java

package com.sample.app;

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

public class ConcurrentModificationDemo1 {
	
	public static void main(String args[]) {
		List<Integer> list = new ArrayList<> ();
		list.add(10);
		list.add(11);
		list.add(12);
		list.add(13);
		list.add(14);
		
		for(Integer i : list) {
			System.out.println(i);
			list.remove(i);
		}
		
	}

}

 

Output

10
Exception in thread "main" java.util.ConcurrentModificationException
	at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:909)
	at java.util.ArrayList$Itr.next(ArrayList.java:859)
	at com.sample.app.ConcurrentModificationDemo1.main(ConcurrentModificationDemo1.java:16)

 

You will get ConcurrentModificationException, by adding new elements to the list while iterating.

 

ConcurrentModificationDemo2.java

package com.sample.app;

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

public class ConcurrentModificationDemo2 {
	public static void main(String args[]) {
		List<Integer> list = new ArrayList<> ();
		list.add(10);
		list.add(11);
		list.add(12);
		list.add(13);
		list.add(14);
		
		for(Integer i : list) {
			System.out.println(i);
			list.add(100);
		}
		
	}
}

 

Output

10
Exception in thread "main" java.util.ConcurrentModificationException
	at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:909)
	at java.util.ArrayList$Itr.next(ArrayList.java:859)
	at com.sample.app.ConcurrentModificationDemo2.main(ConcurrentModificationDemo2.java:15)

 

How to avoid this problems?

a. Do not update the state of the collection while iterating.

 

b. If you want to remove the collection elements while iterating, use iterator remove method.

Iterator<Integer> iter = list.iterator();

while (iter.hasNext()) {
	System.out.println(iter.next());
	iter.remove();
}

 

ConcurrentModificationDemo3.java

package com.sample.app;

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

public class ConcurrentModificationDemo3 {

	public static void main(String args[]) {
		List<Integer> list = new ArrayList<>();
		list.add(10);
		list.add(11);
		list.add(12);
		list.add(13);
		list.add(14);

		System.out.println("List size : " + list.size());

		Iterator<Integer> iter = list.iterator();

		while (iter.hasNext()) {
			System.out.println(iter.next());
			iter.remove();
		}

		System.out.println("List size : " + list.size());
	}
}

 

Output

List size : 5
10
11
12
13
14
List size : 0

 

What is fail-fast iterator?

Iterators that fail by throwing ‘ConcurrentModificationException’ whenever they find a state change in the underlying collection are called fail-fast iterators.

 

Reference

https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/ConcurrentModificationException.html

 

 

You may like

Interview Questions

Implementre-try-catch

NumberFormatException Explained

Initial heap size set to a larger value than the maximum heap size

Is below statement compile?

Versioning History of Java

 

 

 

 

 

No comments:

Post a Comment