+ 5

What is Concurrent Modification ?

18th Nov 2016, 8:28 AM
David Kariuki
David Kariuki - avatar
2 Respuestas
+ 3
java.util.ConcurrentModificationException is a very common exception when working with java collection classes. Java Collection classes are fail-fast, which means if the Collection will be changed while some thread is traversing over it using iterator, the iterator.next() will throw ConcurrentModificationException.
18th Nov 2016, 10:14 AM
Abdelaziz Abubaker
Abdelaziz Abubaker - avatar
0
TechTro answer is perfect. I just want to add that it can also happen in a single thread. It basically happens if you modify a list or other iterable while iterating over it using an iterator. This also happens if you use for (String s:list){ if (s.equals ("") list.remove (s); } because the for loop uses an iterator and while iterating the list is changed. Avoiding this is easy just use a normal index based for loop. int l=list.size (); for (int i=0;i <l;i++){ String s=list.get (i) if (s.equals (""){ list.remove (s); l--;} }
19th Nov 2016, 3:56 PM
No One
No One - avatar