• Post Reply Bookmark Topic Watch Topic
  • New Topic

CopyOnWriteArrayList and 'for construct'  RSS feed

 
Dirk Ooms
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I am using CopyOnWriteArrayList, but i am observing that the code is sporadically throwing a ConcurrentModificationException. This happens when i iterate over the list with a for construct. Should i use the iterator() instead? I haven't seen this documented anywhere.

Error log:
[org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/alfresco].[uploadContent]] Servlet.service() for servlet uploadContent threw
exception java.util.ConcurrentModificationException
at java.util.AbstractList$Itr.checkForComodification(AbstractList.java:449)
at java.util.AbstractList$Itr.next(AbstractList.java:420)
at eu.xenit.alfresco.castor.CastorContentStore.showCurrentListOfKnownCastorNodes(CastorContentStore.java:278)

line 278 contains:
for (String castorNode : castorNodeList) {

in which castorNodeList is declared as:
private List<String> castorNodeList = new CopyOnWriteArrayList<String>();

cheers,
dirk

[edit]Disable smilies. CR[/edit]
 
Campbell Ritchie
Sheriff
Posts: 55351
157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch

Don't know, but it does say in the documentation that the iterators never throw a Concurrent Modification Exception. Probably worth trying an iterator.
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is interesting. The enhanced for loop uses the iterator in the background to do its job, so I would think that since the iterator can't throw the exception neither should the for loop be able to. Still, I would try it to see.


<demo that for loop uses an iterator>
 
Dirk Ooms
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just as a follow-up. The mistake was in my code. After declaring the castorNodeList as a CopyOnWriteArrayList, it was assigned to something of type List, which happened to be an ordinary ArrayList. Sorry for wasting your time. At least i learned that the for loop IS using the iterator, thanks for that post.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!