0

I've been running into a ConcurrentModificationException when running the below code (which is a snippet of the actual loop)

I tried following the answer to this question Concurrent Modification exception but found that the reall issue occurs when I try to split nextLine.

Is there a proper way to do the split in a loop without causing the exception?

 ArrayList<Task> temp = new ArrayList<>();
 ListIterator<String> iterator = taskFileFromJob.listIterator();
 int count = 0;
 while (iterator.hasNext()) {

    String nextLine = iterator.next();
    if (nextLine != null && nextLine.split(Pattern.quote("|")) != null) {

        String[] lineSplit = nextLine.split(Pattern.quote("|"));

        temp.add(new Task(lineSplit[0]));

        if (lineSplit.length > 1) {
           temp.get(count).setDependenciesList(new ArrayList<>(Arrays.asList(lineSplit[1].split(","))));

           if (lineSplit.length > 2) {
              temp.get(count).setDocumentList(new ArrayList<>(Arrays.asList(lineSplit[2].split(","))));
           }
        }
        count++;
    }

 }

EDIT: Here is the full stack trace

java.util.ConcurrentModificationException
    at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:859)
    at java.util.ArrayList$Itr.next(ArrayList.java:831)
    at app.content.ContentController.parseTaskFromJobFile(ContentController.java:121)
    at app.content.ContentController.runLogic(ContentController.java:74)
    at specter2.AppController.loadApp(AppController.java:92)
    at specter2.AppManager$1.actionPerformed(AppManager.java:103)
    at aurora.engine.V1.Logic.AThreadWorker.run(AThreadWorker.java:89)
    at java.lang.Thread.run(Thread.java:745)

EDIT 2:

Seems using a Thread to iterate over this is another possible issue. I tried using syncronized in the method containing this block but I still ended up with the same issue.

I then removed the use of the additional Thread and ended up with the same exception but with a different stack trace:

java.util.ConcurrentModificationException
    at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:859)
    at java.util.ArrayList$Itr.next(ArrayList.java:831)
    at app.content.ContentController.parseTaskFromJobFile(ContentController.java:122)
    at app.content.ContentController.runLogic(ContentController.java:74)
    at specter2.AppController.loadApp(AppController.java:92)
    at specter2.AppManager.launchApp(AppManager.java:103)
    at specter2.AppManager$AppButtonSelectedListener.actionPerformed(AppManager.java:243)
    at aurora.engine.V1.UI.ARadioButton.setSelected(ARadioButton.java:89)
    at aurora.engine.V1.UI.ARadioButton$Click.mousePressed(ARadioButton.java:137)
    at java.awt.Component.processMouseEvent(Component.java:6502)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3320)
    at java.awt.Component.processEvent(Component.java:6270)
    at java.awt.Container.processEvent(Container.java:2229)
    at java.awt.Component.dispatchEventImpl(Component.java:4861)
    at java.awt.Container.dispatchEventImpl(Container.java:2287)
    at java.awt.Component.dispatchEvent(Component.java:4687)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4489)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
    at java.awt.Container.dispatchEventImpl(Container.java:2273)
    at java.awt.Window.dispatchEventImpl(Window.java:2719)
    at java.awt.Component.dispatchEvent(Component.java:4687)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735)
    at java.awt.EventQueue.access$200(EventQueue.java:103)
    at java.awt.EventQueue$3.run(EventQueue.java:694)
    at java.awt.EventQueue$3.run(EventQueue.java:692)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
    at java.awt.EventQueue$4.run(EventQueue.java:708)
    at java.awt.EventQueue$4.run(EventQueue.java:706)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:705)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
Community
  • 1
  • 1
Sammy Guergachi
  • 1,986
  • 4
  • 26
  • 52
  • 3
    Post the stack trace because I doubt that's where the error comes from. – Sotirios Delimanolis Jul 10 '14 at 17:16
  • Have you tried just using a `for` loop? – BitNinja Jul 10 '14 at 17:18
  • @BitNinja Yes, thats what I did initially until I found the linked question and decided to switch to ListIterators – Sammy Guergachi Jul 10 '14 at 17:19
  • 1
    Calling `split` and not assigning the result to anything is a huge waste of a perfectly good source line. What were you expecting this statement to do? – ajb Jul 10 '14 at 17:22
  • If a `for` loop worked then there is no reason to switch to `Iterators` (not that you shouldn't understand them, they can be useful). If you use a `for` loop and are removing items, remember to loop backwards. – BitNinja Jul 10 '14 at 17:22
  • 1
    I strongly suspect you're doing something else within the loop - either that, or you've got multiple threads working, and a different thread is modifying the list. Either way, a short but complete program demonstrating the problem should clear it up. – Jon Skeet Jul 10 '14 at 17:23
  • What line, if any, in the stack trace refers to a line in the Java code you've posted? – ajb Jul 10 '14 at 17:23
  • @ajb This was for testing, I removed the unnecessary stuff that doesn't relate to the exception. This is as short as I can make it and still get the exception. If you think having the whole section under the loop would help I can add it. – Sammy Guergachi Jul 10 '14 at 17:24
  • 4
    Notice how `split` is **not at all involved** in the stack trace. – Sotirios Delimanolis Jul 10 '14 at 17:25
  • @ajb The stack trace is referring to `String nextLine = iterator.next();` It seems that if I removed the `split` line the loop runs fine though. – Sammy Guergachi Jul 10 '14 at 17:25
  • 1
    No, as you've shown it, `split` (and its result) has no bearing on the list or the iterator. – Sotirios Delimanolis Jul 10 '14 at 17:27
  • @SotiriosDelimanolis What I don't get is how the exception is possible if I'm not modifying `taskFileFromJob` but instead iterating over it and then splitting the String and adding them to a different `List`? – Sammy Guergachi Jul 10 '14 at 17:36
  • Can't help without seeing the _actual_ code. All we can say with certainty is that _invoking_ `String#split(..)` is completely irrelevant to the problem. – Sotirios Delimanolis Jul 10 '14 at 17:37
  • 2
    From the trace its executing inside a thread `aurora.engine.V1.Logic.AThreadWorker`. Chances are two thread trying to iterate it. Try using a synchronized block. – Syam S Jul 10 '14 at 17:38
  • 4
    I think you have another thread modifying `taskFileFromJob`. The javadoc for [`ConcurrentModificationException`](http://docs.oracle.com/javase/8/docs/api/java/util/ConcurrentModificationException.html) says that generally a thread can't modify a collection if another thread is iterating over it. If removing the `split` makes this code work, that means it's probably a timing issue--i.e. removing `split` makes the iteration complete faster so that the modification in the other thread happens too late to cause the exception. – ajb Jul 10 '14 at 17:47
  • @ajb I made another Edit to the question reflecting your comments – Sammy Guergachi Jul 10 '14 at 18:04
  • have you checked this : http://stackoverflow.com/questions/1655362/concurrentmodificationexception-java-iterator-next – Abhijeet Panwar Jul 10 '14 at 18:12
  • Since you're copying them to another list anyhow, comment out the iterator.remove() and see if your situation improves. – spudone Jul 11 '14 at 00:07
  • @spudone I tried that and still got the same exception – Sammy Guergachi Jul 11 '14 at 14:55

0 Answers0