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)