Why am I Not Getting a Java.Util.Concurrentmodificationexception in This Example

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

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.

Why im not getting concurrent modification exception during update of collection element?


why updating should cause concurrent modification?

If by "updating" you mean calling the set method, it won't cause concurrent modification.
Setting the value of an element in the List is not a structural modification, and therefore doesn't cause ConcurrentModificationException when performed during iteration.

Quotes from the ArrayList Javadoc:

A structural modification is any operation that adds or deletes one or more elements, or explicitly resizes the backing array; merely setting the value of an element is not a structural modification
...

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. Thus, in the face of concurrent modification, the iterator fails quickly and cleanly, rather than risking arbitrary, non-deterministic behavior at an undetermined time in the future.

Why am I getting java.util.ConcurrentModificationException?


Why am I getting this exception ?

You're removing an item from the list other than via the iterator, while iterating over it. You're also adding to the list while you're iterating over it.

It's not really clear what you're trying to achieve here, but other than with the concurrent collections, you'll always get an exception when you try to do that.

One common fix to this is to create a copy of the list first and iterate over that, modifying the original as you go.

java.util.ConcurrentModificationException But I am not removing

From ConcurrentModificationException Javadoc:

Note that this exception does not always indicate that an object has
been concurrently modified by a different thread. (...) For

example, if a thread modifies a collection directly while it is

iterating over the collection with a fail-fast iterator, the iterator
will throw this exception.

The bug is adding a child into the collection while also iterating over it.

The fault is only detected by the iterator, when it is incremented in the for loop.

How to avoid java.util.ConcurrentModificationException when iterating through and removing elements from an ArrayList

Two options:

  • Create a list of values you wish to remove, adding to that list within the loop, then call originalList.removeAll(valuesToRemove) at the end
  • Use the remove() method on the iterator itself. Note that this means you can't use the enhanced for loop.

As an example of the second option, removing any strings with a length greater than 5 from a list:

List<String> list = new ArrayList<String>();
...
for (Iterator<String> iterator = list.iterator(); iterator.hasNext(); ) {
String value = iterator.next();
if (value.length() > 5) {
iterator.remove();
}
}

Sometimes receiving java.util.ConcurrentModificationException, sometimes not. Can't figure out why

The problems happens because your code modifies the list while iterating.

  • If you have only one thread which reads/writes from/to the list, then the most likely the call

    this.createBufferStrategy(2);

sometimes adds some elements to the same array list. You could fix the exception if you will use a copy of the list to iterate:

for (Objects e : new ArrayList(list)) {
e.render(g);
}
  • If you have MORE than one thread to read/write the list, use java.util.concurrent.CopyOnWriteArrayList instead

java.util.ConcurrentModificationException not thrown when expected

The iterator returned from ArrayList.iterator() in the implementation we're apparently both using only checks for structural modification in calls to next(), not in calls to hasNext(). The latter just looks like this (under Java 8):

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

So in your second case, the iterator "knows" that it's returned two elements, and that the list only has two elements... so hasNext() just returns false, and we never end up calling next() the third time.

I would view this as an implementation detail - basically the checking not being as strict as it could be. It would be entirely reasonable for hasNext() to perform a check and throw an exception in this case too.

Why do I get a ConcurrentModificationException even though I do not edit anything?

According to the Java specification, get(..) should not throw a ConcurrentModificationException if the SortedMap has not been structurally modified. As you have not provided the full code, the only thing I can suggest is to wrap the SortedMap with Collections.unmodifiableSortedMap(S s) after you create it. This will throw an Exception if you have missed code that structurally modifies the Map.

Even if the code you provided does not modify the SortedMap, this doesn't exclude that an external(out of method)thread does not modify it.



Related Topics



Leave a reply



Submit