Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

Concurrent Modification Exception

  Asked By: Anita    Date: Sep 20    Category: Java    Views: 1529

I'm working with a very large text file which I've read into an ArrayList.
Now I need to start working with
the ArrayList. I've set up an Iterator for it, but I keep getting a
Concurrent Modification Exception at
runtime. Here's a code snippet:

in = new BufferedReader(new FileReader("word.lst"));
out = new PrintWriter(new FileWriter("results.txt"));

ArrayList words = new ArrayList();
Iterator wordsOrder = words.iterator();

//do initial load of data
while (in.readLine() != null) {

while (wordsOrder.hasNext()){
input = (String) wordsOrder.next();

I'm getting the exception on the line where I downcast the next entry of
the ArrayList to a String (the
last substantive line above). I'm only running the one thread, so it
doesn't seem to be thread-related.
The input file is a very long list of words delimited with linebreaks. I've
tried using a LinkedList and
a HashSet instead of the ArrayList, but keep getting the same exception.

Does anybody have any ideas?



3 Answers Found

Answer #1    Answered By: Guadalupe Rogers     Answered On: Sep 20

Move the iterator  declaration to here. You are declaring an iterator, then
trying to modify the container. Once you have your iterator, any changes to
the underlying container could trigger a ConcurrentModificationException.
From the Sun's java documentation:

"This field (modCount) is used by the iterator and list  iterator
implementation returned by the iterator and listIterator methods. If the
value of this field changes unexpectedly, the iterator (or list iterator)
will throw a ConcurrentModificationException in response to the next, remove,
previous, set  or add operations. This provides fail-fast behavior, rather
than non-deterministic behavior in the face of concurrent modification  during

Answer #2    Answered By: Gustavo Taylor     Answered On: Sep 20

Actually, here a better quote:

"The iterators returned by this class's (ArrayList) iterator  and listIterator
methods are fail-fast: if 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. "

Answer #3    Answered By: Velma Adams     Answered On: Sep 20

I would strongly suggest to have these two loops strictly divided
from each other. That means:
1) Have one loop reading the whole file  into the ArrayList.
2) Set up the iterator.
3) Have one loop processing the content of the ArrayList.

It really should be that easy.

Another approach (handy if you insist on having the strings from the
text file processed _and_ at the same time stored in the ArrayList)
would go like this:
Modify your loop such that the string read  from the input  file is
processed immediately and then only stored in the ArrayList; just
NEVER try to append something to an ArrayList which you want to
process out of this same ArrayList at the same time. That can't work.

I hope I made myself clear. If not, please ask again. I'm just too
plain tired to rewrite the loop now, so I leave that up to you.

Didn't find what you were looking for? Find more on Concurrent Modification Exception Or get search suggestion and latest updates.