提问者:小点点

编辑:javafx:异常并发修改如何处理


我收集在列表视图中检查的所有任务,并在更新列表视图后(添加新元素到列表中,它会刷新/重新加载,所以我从GUI中删除所有值,这些值是用任务键选择的,并尝试通过手动设置复选框任务的值来再次添加新对象。

public synchronized static void getCollectVCF() {
        testVCF = false;
        if (geneBowserFlag == false) {
            checklist.setDisable(true);
        } else {
            checklist.setDisable(false);
        }

        taskNames = LoadVCFFileToTrack.tempVcfList.toArray(new String[LoadVCFFileToTrack.tempVcfList.size()]);
        reactionStrings = new ConcurrentHashMap<String, HBox>();
        if (LoadVCFFileToTrack.tempVcfList.size() > 0) {
            int count = 0;
            for (String s : taskNames) {
                if (LoadVCFFileToTrack.flagBrowse == false || LoadVCFFileToTrack.CollectedPath != null) {
                    if (count <= taskNames.length) {
                        for (String s1 : LoadVCFFileToTrack.CollectedPath) {
                            if (s.equalsIgnoreCase(org.apache.commons.lang3.StringUtils.substringAfterLast(s1, "/"))) {
                                ReadVCFFile.tableGenrate(s1);
                                reactionStrings.put(s, GetSequences.call(LoadGenomeSequence.start,
                                        LoadGenomeSequence.end, ReadVCFFile.collectMultipleObj));
                                System.out.println(
                                        "value : " + s + "\tCountSize  : " + ReadVCFFile.collectMultipleObj.size());
                                VCFTableObj.put(org.apache.commons.lang3.StringUtils.substringAfterLast(s1, "/"),
                                        ReadVCFFile.collectMultipleObj);
                                count++;
                            }

                        }
                    }
                    for (String s1 : TrackFile.listOFVCF) {
                        if (s.equalsIgnoreCase(org.apache.commons.lang3.StringUtils.substringAfterLast(s1, "/"))) {
                            ReadVCFFile.tableGenrate(s1);
                            reactionStrings.put(s, GetSequences.call(LoadGenomeSequence.start, LoadGenomeSequence.end,
                                    ReadVCFFile.collectMultipleObj));
                            System.out.println("value list of File: " + s + "\tCountSize  : "
                                    + ReadVCFFile.collectMultipleObj.size());
                            VCFTableObj.put(org.apache.commons.lang3.StringUtils.substringAfterLast(s1, "/"),
                                    ReadVCFFile.collectMultipleObj);
                            count++;

                        }
                    }
                }
            }
        }
        reactionStringsvcf = new ConcurrentHashMap<String, HBox>();
        {
            for (String s : taskNames) {
                reactionStringsvcf.put(s, GetSequences.TitleBox(s));
            }
        }

        tasks2 = FXCollections
                .observableArrayList(Arrays.stream(taskNames).map(Task::new).collect(Collectors.toList()));

        for (Entry<String, HBox> entry : checkSelectedValuesVCF.entrySet()) {
            for (Task t : tasks2) {
                String s = t.name.toString();
                s = org.apache.commons.lang.StringUtils.substringAfterLast(s, ":");
                s = org.apache.commons.lang.StringUtils.substringBeforeLast(s, "]");

                if (entry.getKey().trim().equalsIgnoreCase(s.trim())) {
                    Platform.runLater(new Runnable() {
                        @Override
                        public void run() {
                            GenerateDraggableGUI.mainbox.getChildren().remove(entry.getValue());
                            CollectedSeq.remove(entry.getKey());
                            t.setSelected(true);
                            testVCF = true;
                        }
                    });
                }
            }
        }

        synchronized (TitledPanes.class) {

            tasks2.forEach(task -> task.selectedProperty().addListener((observable, wasSelected, isSelected) -> {
                VCFMapLocal = new LinkedHashMap<String, HBox>();
                if (isSelected) {
                    GenerateDraggableGUI.mainbox.getChildren().add(reactionStringsvcf.get(task.getName()));
                    VCFMap.put(reactionStringsvcf.get(task.getName()), reactionStrings.get(task.getName()));
                    VCFMapLocal.put((task.getName()), reactionStrings.get(task.getName()));
                    checkSelectedValuesVCF.put(task.getName(), reactionStringsvcf.get(task.getName()));
                    SetOfAll.put(reactionStringsvcf.get(task.getName()), reactionStrings.get(task.getName()));
                    CollectedSeq.add(task.getName());
                    GenerateDraggableGUI.LoadGUIVCF();
                }
                if (wasSelected) {
                    GenerateDraggableGUI.mainbox.getChildren().remove(reactionStringsvcf.get(task.getName()));
                    GenerateDraggableGUI.mainContent.getChildren().remove(reactionStrings.get(task.getName()));
                    VCFMap.remove(reactionStringsvcf.get(task.getName()), reactionStrings.get(task.getName()));
                    SetOfAll.remove(reactionStringsvcf.get(task.getName()), reactionStrings.get(task.getName()));
                    checkSelectedValuesVCF.remove(task.getName());
                    CollectedSeq.remove(task.getName());
                    GenerateDraggableGUI.UnLoadGUI();
                }
            }));
        }
Exception in thread "JavaFX Application Thread" java.util.ConcurrentModificationException
    at java.util.WeakHashMap$HashIterator.nextEntry(WeakHashMap.java:806)
    at java.util.WeakHashMap$EntryIterator.next(WeakHashMap.java:845)
    at java.util.WeakHashMap$EntryIterator.next(WeakHashMap.java:843)
    at com.sun.javafx.css.StyleManager.forget(StyleManager.java:520)
    at javafx.scene.Parent.scenesChanged(Parent.java:652)
    at javafx.scene.Node.invalidatedScenes(Node.java:832)
    at javafx.scene.Node.setScenes(Node.java:898)
    at javafx.scene.Parent.scenesChanged(Parent.java:656)
    at javafx.scene.Node.invalidatedScenes(Node.java:832)
    at javafx.scene.Node.setScenes(Node.java:898)
    at javafx.scene.Parent.scenesChanged(Parent.java:656)
    at javafx.scene.Node.invalidatedScenes(Node.java:832)
    at javafx.scene.Node.setScenes(Node.java:898)
    at javafx.scene.Parent.scenesChanged(Parent.java:656)
    at javafx.scene.Node.invalidatedScenes(Node.java:832)
    at javafx.scene.Node.setScenes(Node.java:898)
    at javafx.scene.Parent$2.onProposedChange(Parent.java:495)
    at com.sun.javafx.collections.VetoableListDecorator.remove(VetoableListDecorator.java:329)
    at com.sun.javafx.collections.VetoableListDecorator.remove(VetoableListDecorator.java:221)
    at org.ArrayGen.FXFeatures.TitledPanes$4.run(TitledPanes.java:418)
    at com.sun.javafx.application.PlatformImpl.lambda$null$170(PlatformImpl.java:295)
    at com.sun.javafx.application.PlatformImpl$$Lambda$67/1869691259.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$171(PlatformImpl.java:294)
    at com.sun.javafx.application.PlatformImpl$$Lambda$65/22805895.run(Unknown Source)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
    at com.sun.glass.ui.gtk.GtkApplication.lambda$null$48(GtkApplication.java:139)
    at com.sun.glass.ui.gtk.GtkApplication$$Lambda$52/1429880200.run(Unknown Source)
    at java.lang.Thread.run(Thread.java:745)

共1个答案

匿名用户

当您在迭代其元素时尝试修改Collection时会出现此异常。为了避免这种情况,您可以使用集合的迭代器(Collection#iterator())遍历元素,然后您可以调用iterator#删除()来删除当前活动的项目。

有用链接:

http://www.javacodegeeks.com/2011/05/avoid-concurrentmodificationexception.html