Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why am I not getting a java.util.ConcurrentModificationException in this example?

Note: I am aware of the Iterator#remove() method.

In the following code sample, I don't understand why the List.remove in main method throws ConcurrentModificationException, but not in the remove method.

public class RemoveListElementDemo {         private static final List<Integer> integerList;      static {         integerList = new ArrayList<Integer>();         integerList.add(1);         integerList.add(2);         integerList.add(3);     }      public static void remove(Integer toRemove) {         for(Integer integer : integerList) {             if(integer.equals(toRemove)) {                                 integerList.remove(integer);             }         }     }      public static void main(String... args) {                         remove(Integer.valueOf(2));          Integer toRemove = Integer.valueOf(3);         for(Integer integer : integerList) {             if(integer.equals(toRemove)) {                                 integerList.remove(integer);             }         }     } } 
like image 885
Bhesh Gurung Avatar asked Nov 18 '11 21:11

Bhesh Gurung


People also ask

How do I fix Java Util ConcurrentModificationException?

How do you fix Java's ConcurrentModificationException? There are two basic approaches: Do not make any changes to a collection while an Iterator loops through it. If you can't stop the underlying collection from being modified during iteration, create a clone of the target data structure and iterate through the clone.

How do you cause ConcurrentModificationException?

Class ConcurrentModificationException. This exception may be thrown by methods that have detected concurrent modification of an object when such modification is not permissible. For example, it is not generally permissible for one thread to modify a Collection while another thread is iterating over it.

What causes Java Util ConcurrentModificationException?

What Causes ConcurrentModificationException. The ConcurrentModificationException generally occurs when working with Java Collections. The Collection classes in Java are very fail-fast and if they are attempted to be modified while a thread is iterating over it, a ConcurrentModificationException is thrown.

Can we get ConcurrentModificationException?

The ConcurrentModificationException occurs when an object is tried to be modified concurrently when it is not permissible. This exception usually comes when one is working with Java Collection classes. For Example - It is not permissible for a thread to modify a Collection when some other thread is iterating over it.


Video Answer


1 Answers

Here's why: As it is says in the Javadoc:

The iterators returned by this class's iterator and listIterator methods are fail-fast: if the list is structurally modified at any time after the iterator is created, in any way except through the iterator's own remove or add methods, the iterator will throw a ConcurrentModificationException.

This check is done in the next() method of the iterator (as you can see by the stacktrace). But we will reach the next() method only if hasNext() delivered true, which is what is called by the for each to check if the boundary is met. In your remove method, when hasNext() checks if it needs to return another element, it will see that it returned two elements, and now after one element was removed the list only contains two elements. So all is peachy and we are done with iterating. The check for concurrent modifications does not occur, as this is done in the next() method which is never called.

Next we get to the second loop. After we remove the second number the hasNext method will check again if can return more values. It has returned two values already, but the list now only contains one. But the code here is:

public boolean hasNext() {         return cursor != size(); } 

1 != 2, so we continue to the next() method, which now realizes that someone has been messing with the list and fires the exception.

Hope that clears your question up.

Summary

List.remove() will not throw ConcurrentModificationException when it removes the second last element from the list.

like image 140
pushy Avatar answered Sep 19 '22 07:09

pushy