分割String时出现ConcurrentModificationException

时间:2014-07-10 17:15:04

标签: java split iterator concurrentmodification

运行以下代码时我遇到了ConcurrentModificationException(这是实际循环的片段)

我尝试按照此问题Concurrent Modification exception的答案,但发现当我尝试拆分nextLine时会出现重新发送问题。

是否有正确的方法在循环中进行拆分而不会导致异常?

 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++;
    }

 }

编辑:这是完整的堆栈跟踪

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)

编辑2:

似乎使用Thread来迭代这是另一个可能的问题。我尝试在包含此块的方法中使用syncronized,但我仍然遇到了同样的问题。

然后我删除了附加线程的使用并最终得到了相同的异常,但堆栈跟踪不同:

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)

0 个答案:

没有答案