Why isn't this code causing a ConcurrentModificationException? [duplicate] - java

This question already has answers here:
Why is a ConcurrentModificationException thrown and how to debug it
(8 answers)
java.util.ConcurrentModificationException not thrown when expected
(2 answers)
Closed 3 years ago.
I was reading about ConcurrentModificationException and how to avoid it. Found an article. The first listing in that article had code similar to the following, which would apparently cause the exception:
List<String> myList = new ArrayList<String>();
myList.add("January");
myList.add("February");
myList.add("March");
Iterator<String> it = myList.iterator();
while(it.hasNext())
{
String item = it.next();
if("February".equals(item))
{
myList.remove(item);
}
}
for (String item : myList)
{
System.out.println(item);
}
Then it went on to explain how to solve the problem with various suggestions.
When I tried to reproduce it, I didn't get the exception! Why am I not getting the exception?

According to the Java API docs Iterator.hasNext does not throw a ConcurrentModificationException.
After checking "January" and "February" you remove one element from the list. Calling it.hasNext() does not throw a ConcurrentModificationException but returns false. Thus your code exits cleanly. The last String however is never checked. If you add "April" to the list you get the Exception as expected.
import java.util.List;
import java.util.ArrayList;
import java.util.Iterator;
public class Main {
public static void main(String args[]) {
List<String> myList = new ArrayList<String>();
myList.add("January");
myList.add("February");
myList.add("March");
myList.add("April");
Iterator<String> it = myList.iterator();
while(it.hasNext())
{
String item = it.next();
System.out.println("Checking: " + item);
if("February".equals(item))
{
myList.remove(item);
}
}
for (String item : myList)
{
System.out.println(item);
}
}
}
http://ideone.com/VKhHWN

From ArrayList source (JDK 1.7):
private class Itr implements Iterator<E> {
int cursor; // index of next element to return
int lastRet = -1; // index of last element returned; -1 if no such
int expectedModCount = modCount;
public boolean hasNext() {
return cursor != size;
}
#SuppressWarnings("unchecked")
public E next() {
checkForComodification();
int i = cursor;
if (i >= size)
throw new NoSuchElementException();
Object[] elementData = ArrayList.this.elementData;
if (i >= elementData.length)
throw new ConcurrentModificationException();
cursor = i + 1;
return (E) elementData[lastRet = i];
}
public void remove() {
if (lastRet < 0)
throw new IllegalStateException();
checkForComodification();
try {
ArrayList.this.remove(lastRet);
cursor = lastRet;
lastRet = -1;
expectedModCount = modCount;
} catch (IndexOutOfBoundsException ex) {
throw new ConcurrentModificationException();
}
}
final void checkForComodification() {
if (modCount != expectedModCount)
throw new ConcurrentModificationException();
}
}
Every modifying operation on an ArrayList increments the modCount field (the number of times the list has been modified since creation).
When an iterator is created, it stores the current value of modCount into expectedModCount. The logic is:
if the list isn't modified at all during the iteration, modCount == expectedModCount
if the list is modified by the iterator's own remove() method, modCount is incremented, but expectedModCount gets incremented as well, thus modCount == expectedModCount still holds
if some other method (or even some other iterator instance) modifies the list, modCount gets incremented, therefore modCount != expectedModCount, which results in ConcurrentModificationException
However, as you can see from the source, the check isn't performed in hasNext() method, only in next(). The hasNext() method also only compares the current index with the list size. When you removed the second-to-last element from the list ("February"), this resulted that the following call of hasNext() simply returned false and terminated the iteration before the CME could have been thrown.
However, if you removed any element other than second-to-last, the exception would have been thrown.

I think the correct explanation is this extract from the javadocs of ConcurrentModificationExcetion:
Note that fail-fast behavior cannot be guaranteed as it is, generally
speaking, impossible to make any hard guarantees in the presence of
unsynchronized concurrent modification. Fail-fast operations throw
ConcurrentModificationException on a best-effort basis. Therefore, it
would be wrong to write a program that depended on this exception for
its correctness: ConcurrentModificationException should be used only
to detect bugs.
So, if the iterator is fail fast it may throw the exception but thereĀ“s no guarantee. Try replacing February with January in your example and the exception is thrown (At least in my environment)

The iterator checks that it has iterated as many times as you have elements left to see it has reached the end before it checks for a concurrent modification. This means if you remove only the second last element you don't see a CME in the same iterator.

Related

Why ConcurentModificationException isn't always thrown when we remove element in a for loop [duplicate]

This question already has an answer here:
ConcurrentModificationException not thrown consistently
(1 answer)
Closed 1 year ago.
G'day everyone. Today I get a quite weird situations regarding ConcurentModificationException.
I have a list like this.
ArrayList<Integer> list = new ArrayList<Integer>();
list.add(1);
list.add(2);
list.add(3);
I also have three pieces of code like this.
This will print [2, 3].
for (int i = 0; i < list.size(); i++) {
Integer integer = list.get(i);
if (integer.equals(1)) {
list.remove(integer);
}
}
System.out.println(list);
This will throw exception.
for (Integer integer : list) {
if (integer.equals(1)) {
list.remove(integer);
}
}
System.out.println(list);
This will print [1, 3].
for (Integer integer : list) {
if (integer.equals(2)) {
list.remove(integer);
}
}
System.out.println(list);
I used to think that removing an element inside a for-loop always results ConcurrentModificationException but I might be wrong. Can you guys tell me what create the difference here.
I run the code on Corretto 11.
The point here is that the ConcurrentModificationException is thrown when you access(!) the next element via the iterator, not when checking for the existance of a further element.
In the second snippet you remove the first element then there are still two to go. For the next iteration it will be noticed, that there are more elements which will be accessed and as the expectedModCount changed, the Exception will be thrown.
In the third example however, due to the reduced size, you will have reached the end after the second iteration and no further access will take place and therefore no exception is thrown.
The first example is not meant to throw an exception, because you are not using an iterator, which would check for the concurrent modification in the first place.
As was mentioned earlier first code snippet is not supposed to throw ConcurrentModificationException, because you are not iterating over list.
As regards to other two snippets the equivalent code is following:
ArrayList<Integer> list = new ArrayList<Integer>();
list.add(1);
list.add(2);
list.add(3);
Iterator it = list.iterator();
while(it.hasNext()) {
Integer integer = (Integer)it.next();
if (integer.equals(2)) {
list.remove(integer);
}
}
System.out.println(list);
and if you take a look at java.util.ArrayList.Itr implementation:
public boolean hasNext() {
return cursor != size;
}
#SuppressWarnings("unchecked")
public E next() {
checkForComodification();
int i = cursor;
if (i >= size)
throw new NoSuchElementException();
Object[] elementData = ArrayList.this.elementData;
if (i >= elementData.length)
throw new ConcurrentModificationException();
cursor = i + 1;
return (E) elementData[lastRet = i];
}
you can find out that the reason of such behaviour is caused by cursor = i + 1 in next() call: removing penultimate element in ArrayList decreases size and followed hasNext() call returns false.
A regular for loop and an enhanced one are, slightly, different.
While the regular one loops over the list and gets the individual elements one by one, the enhanced one uses an Iterator. The enhanced for loop works with Iterable objects or an array.
Basically the enhanced for loop translates to this
for(Iterator<Integer> it = list.iterator(); it.hasNext(); ) {
Integer integer = it.next();
if (integer.equals(2)) {
list.remove(integer);
}
}
This also explains why the first enhanced loop throws an exception. As according to the iterator there should be more elements. Calling the next method detects that the underlying collection has changed and thus throws a ConcurrentModificationException.
The last one doesn't because you are already at the end of the iterator and thus no additional call to next.
When you remove an element at index 0, the list shrinks, therefore your list with [1,2,3] is now [2,3]. you then remove index 1 wich remove the 3. You have now [2] in your list. Then you remove index 2 which no longer exist.
You may want to remove only index 0 at every iteration or remove from the last index to the first.

Why I'm not getting ConcurrentModificationException while removing element from ArrayList during iteration [duplicate]

This question already has answers here:
Why no ConcurrentModificationException in this situation with LinkedList iterator? [duplicate]
(2 answers)
Closed 3 years ago.
I am using the following code to loop through an arraylist and then removing one element from the arraylist.
Here i'm expecting ConcurrentModificationException. But didn't get that exception. especially when you are checking condition with (n-1)th element. Please help me. Below is my code.
ArrayList<Integer> arrayList = new ArrayList<Integer>();
for (int i = 1; i <= 10; i++) {
arrayList.add(5 * i);
}
System.out.println(arrayList);
Iterator<Integer> iterator = arrayList.iterator();
while (iterator.hasNext()) {
Integer temp = iterator.next();
if (temp == 45) {
/**
* temp == 40 (then i'm getting *ConcurrentModificationException) why not i'm
* getting ConcurrentModificationException if (temp == 45)
*/
arrayList.remove(1);
}
}
System.out.println(arrayList);
Thanks in Advance
The implementation makes a best effort to detect concurrent modification, but there are cases where it fails to do so.
The Iterator implementation returned for ArrayList's Iterator checks for concurrent modification in next() and remove(), but not in hasNext(), whose logic is:
public boolean hasNext() {
return cursor != size;
}
Since you removed an element when the Iterator's cursor was at the element before the last element, the removal causes hasNext() to return false (since size becomes equal to cursor after the removal), which ends your loop without throwing an exception.

Remove while iterating leaves element and does not throw Concurrent E

I encountered this piece a of code, that removes item from list while iterating, and yet it does not throw concurrent exception and leaves some items in the list.
Why is that?
Example:.
public class Application {
public static void main(String[] args) {
List<Integer> integerList = new ArrayList<Integer>();
integerList.add(1);
integerList.add(2);
for (Integer integer : integerList) {
removeInteger(integerList, integer);
}
System.out.println(integerList.toString()); // prints [2] as it was not removed (while it should be)
}
private static void removeInteger(List<Integer> integerList, Integer integer) {
for (Integer integer1 : integerList) {
if (integer.equals(integer1)) {
int index = integerList.indexOf(integer);
if (index >= 0) {
integerList.remove(index);
}
}
}
}
}
If I change removeInteger method to use Iterator instead and in main I pass a copy of integerList everything works as expected.
First of all if you add some more elements to the list it will still fail with a ConcurrentModificationException.
For the reason that it does not fail with the posted example, we have to take a look at what happens internally.
If you debug you see that in the posted example, both loops are only iterated once. Removing the element in the inner loop (removeInteger method) exhausts both iterators. As there is no further interaction, no error is caused. 'Only' a call of Iterator#next after a modification could cause a ConcurrentModificationException.
Yes, but why does it exhaust them?
Checking the source code of ArrayList#Itr we can see that
public boolean hasNext() {
return cursor != size;
}
only checks a variable called size. This variable size is a property of the ArrayList. When remove is called this variable is decremented. Hence the next time hasNext is called, the Iterator assumes there are no more new elements.
SideNote: cursor points at the next element in the list.

Is iterator.next modified if the underlying list is modified with LinkedList add method?

intLinkList = 2, 4, 6, 8, 10
Iterator itr = intLinkList.iterator()
Say the iterator is iterating and currently pointing at Integer 6.
itr current item = 6
itr previous item = 4
itr next item = 8
When the itr is currently pointing at Integer 6, I use the Linklists' add(Object obj, int index) method to add/insert Integer 7 between Integer 6 and Integer 8.
I understand this itr instance is invalid after this modification because the underlying list has been modified, hence modCount != expectedModCount.
My question is this:
Does the modification using LinkedList's add method change the item the itr.next is pointing at?
I did some reading and I know this will throw a ConcurrentModificationException.
But this does not answer my question if the itr.next item is modified if the underlying list get modified while iterator is iterating.
Does the modification using LinkedList's add method change the item the itr.next is pointing at?
No.
Calling LinkedList's add doesn't change anything in the Iterator's state. Once you call, the Iterator's next() method, and before the iterator calculates the next element to return, it will check for modification and throw the ConcurrentModificationException.
Here's the relevant code, from AbstractList$Itr:
public E next() {
checkForComodification(); // will throw ConcurrentModificationException
// before the Iterator's state is changed
try {
int i = cursor;
E next = get(i);
lastRet = i;
cursor = i + 1;
return next;
} catch (IndexOutOfBoundsException e) {
checkForComodification();
throw new NoSuchElementException();
}
}
A key detail is that Java only has primitives and references.
When you add something to a List or any collection, it is a copy of the reference.
If you modify the object referenced, the collection is not modified though if you print the contents it might appear so.
If you call add or remove on a collection, for LinkedList and ArrayList, the iterator isn't modified but can no longer iterate (there is one exception)
If you use a CopyOnWriteArrayList you can modify the collection and keep iterating, however the iterator doesn't see the change.

Understanding concurrentModificationException and implementation of ArrayList

I tried to reproduce ConcurrentModificationException myself by writing the following code:
List<String> last = new ArrayList<>();
last.add("a");
last.add("b");
for(String i : last){
System.out.println(i);
last.remove(i);
}
System.out.println(last);
DEMO
Since, the documentation of ArrayList mentioned
Note that the fail-fast behavior of an iterator cannot be guaranteed
as it is, generally speaking, impossible to make any hard guarantees
in the presence of unsynchronized concurrent modification.
I expected that in single-threaded programs such detection is straghtforward. But the program printed
a
[b]
instead. Why?
Your code is equivalent to the following:
List<String> last = new ArrayList<>();
last.add("a");
last.add("b");
for(Iterator<String> i = last.iterator(); i.hasNext(); ) {
String value = i.next();
System.out.println(value);
last.remove(value);
}
System.out.println(last);
The flow of the for loop is:
System.out.println(value); // prints "a"
last.remove(value); // removes "a" from the list
i.hasNext() // exits the loop, since i.hasNext() is false
System.out.println(last); // prints "[b]" - the updated list
That's why you get your output and no ConcurrentModificationException.
You will get ConcurrentModificationException if you'll add another value to your list (e.g. last.add("c")), because then i.hasNext() will be true after the first iteration and i.next() will throw the exception.
As explained in the documentation:
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
Because you are looping through the list using the new syntax for(String i : last) an iterator is created for you and you can't modify the list while looping it.
This exception is not related to multithreading. Also working with only one thread you can throw that exception.
Internally there is a variable modCount that is incremented for every modification to the structure of the list. When the iterator is first created it saves that value of modCount in a variable expectedModCount. Every subsequent modification check if that value of expectedModCount is equal to modCount. If not a ConcurrentModificationException is thrown.
I add the code of remove as an example. The same for add, addAll and all others methods that modify the list.
public E remove(int index) {
rangeCheck(index);
// Check if a modification should thrown a ConcurrentModificationException
checkForComodification();
E result = parent.remove(parentOffset + index);
this.modCount = parent.modCount;
this.size--;
return result;
}
final void checkForComodification() {
if (expectedModCount != ArrayList.this.modCount)
throw new ConcurrentModificationException();
}
}
Itr class in ArrayList class has following methods
public boolean hasNext() {
return cursor != size;
}
final void checkForComodification() {
if (modCount != expectedModCount)
throw new ConcurrentModificationException();
}
Here, modCount is the number of times list has been structurally modified. When we create for loop, internally an iterator will be created and expectedModCount will be initialized to modCount.
When there are only 2 elements in the list and after removing one element, for loop will check the condition using hasNext() method call. So, condition cursor != size (1!=1) will be met first. Hence, loop won't proceed further and ConcurrentModificationException will not be thrown.
But, when there are 1,3,4 etc number of elements are there in the list then, for loop will proceed further after hasNext() method call. But, while fetching the element using next() method inside for loop, it will call checkForComodification() and condition modCount != expectedModCount will be met. Hence, exception will be thrown.
For two values, it will not fail because the for loop will be exited. Add a third element and you will get java.util.ConcurrentModificationException
List<String> last = new ArrayList<>();
last.add("a");
last.add("b");
last.add("c");
for(String i : last){
System.out.println(i);
last.remove(i);
}
System.out.println(last);

Categories

Resources