Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

ConcurrentModificationException  RSS feed

 
Arun Giridhar
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello ,

Following is the code snippet where i'm getting this exception gone through alot of SO/articles could not tell whats going wrong, can some rancher guide me.



stacktrace

Exception in thread "main" java.util.ConcurrentModificationException
at java.util.ArrayList$SubList.checkForComodification(Unknown Source)
at java.util.ArrayList$SubList.listIterator(Unknown Source)
at java.util.AbstractList.listIterator(Unknown Source)
at java.util.ArrayList$SubList.iterator(Unknown Source)
at com.home.fun.me.CollectMe.main(CollectMe.java:193)


thank you
 
Gregg Marcus
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Arun,

You are getting a ConcurrentModificationException exception. This occurs when the array list is structurally modified any time after the iterator is created in any way except through the iterator's own add/remove methods.

Using the code example below, notice that I have added three names (Gregg, Arun and Venkatesh). Next I retrieved the iterator from the ArrayList. Finally, I try to add one more name (Ahmed) after retrieving the iterator. At this point, the iterator will throw a Concurrent Modification Exception if you try to add additional elements to the arraylist using the arraylist.add(). The same is true for removing elements.



In the example you provide, you are calling arlPerson.addAll(arlPerson1) on line 11. This is what triggers the ConcurrentModificationException.

Please search for "java fast fail iterator example" for more information.
 
Henry Wong
author
Sheriff
Posts: 22846
119
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gregg Marcus wrote:
You are getting a ConcurrentModificationException exception. This occurs when the array list is structurally modified any time after the iterator is created in any way except through the iterator's own add/remove methods.


Actually, while what you are describing is the most common single-threaded case, that is not the issue here.

The issue has to do with line three of the code. Once a subList() is taken, the original list can't be structurally modified. This is because the sub list is only a view into the main list.

If the main list is structurally modified, then the sub list is declared invalid, and any calls on the sub list will generate the error. And this happens at line 5.

Henry
 
Gregg Marcus
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is fascinating - I didnt realize. Thanks for making the correction!
 
Arun Giridhar
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:

The issue has to do with line three of the code. Once a subList() is taken, the original list can't be structurally modified. This is because the sub list is only a view into the main list.

Henry


Why the sublist is a view ?
 
Henry Wong
author
Sheriff
Posts: 22846
119
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Arun Giridhar wrote:
Why the sublist is a view ?


As described in the JavaDoc ... http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html#subList(int,%20int)

Henry
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!