JavaFX图表系列ConcurrentModificationException

时间:2016-05-02 17:49:04

标签: java javafx task

我在JavaFX上构建遗传算法,我希望有一个图表来更新每一代的平均值和最大适应度。 我有一个按钮,可以完成20代,每次迭代都会更新图形,还有进度条。

代码过去比较简单,没有所有重复和引用。这些只是关于如何避免异常的想法。但没有任何效果

我的意思是,我甚至不会迭代或删除某些内容。这只是添加。

我得到了这个例外:

Exception in thread "JavaFX Application Thread" java.util.ConcurrentModificationException
    at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:901)
    at java.util.ArrayList$Itr.next(ArrayList.java:851)
    at java.util.Collections$UnmodifiableCollection$1.next(Collections.java:1042)
    at javafx.scene.chart.AreaChart.layoutPlotChildren(AreaChart.java:451)
    at javafx.scene.chart.XYChart.layoutChartChildren(XYChart.java:731)
    at javafx.scene.chart.Chart$1.layoutChildren(Chart.java:94)
    at javafx.scene.Parent.layout(Parent.java:1079)
    at javafx.scene.Parent.layout(Parent.java:1085)
    at javafx.scene.Parent.layout(Parent.java:1085)
    at javafx.scene.Parent.layout(Parent.java:1085)
    at javafx.scene.Scene.doLayoutPass(Scene.java:552)
    at javafx.scene.Scene$ScenePulseListener.pulse(Scene.java:2397)
    at com.sun.javafx.tk.Toolkit.lambda$runPulse$31(Toolkit.java:348)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.tk.Toolkit.runPulse(Toolkit.java:347)
    at com.sun.javafx.tk.Toolkit.firePulse(Toolkit.java:374)
    at com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:510)
    at com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:490)
    at com.sun.javafx.tk.quantum.QuantumToolkit.lambda$runToolkit$405(QuantumToolkit.java:319)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$149(WinApplication.java:191)
    at java.lang.Thread.run(Thread.java:745)

以下是准备工作

public void prepareChart() {
    final NumberAxis xAxis = new NumberAxis();
    final NumberAxis yAxis = new NumberAxis();
    final AreaChart<Number, Number> graph = new AreaChart<Number, Number>(xAxis, yAxis);
    graph.setCreateSymbols(false);
    graph.setTitle("Fitness - Generations relation");
    graph.getXAxis().setLabel("Generations");
    graph.getYAxis().setLabel("Fitness");

    final Series<Number, Number> averageFitness = new XYChart.Series<Number, Number>();
    averageFitness.setName("Average fitness");

    final Series<Number, Number> maximumFitness = new XYChart.Series<Number, Number>();
    maximumFitness.setName("Maximum fitness");
    graph.getData().addAll(averageFitness, maximumFitness);
    graphBox.getChildren().add(graph);
    graphRef = graph;
}

以下是我添加数据的方式

public void addData(double avgFitness, double maxFitness) {
    final Series<Number, Number> averageFitness = graphRef.getData().get(0);
    final Series<Number, Number> maximumFitness = graphRef.getData().get(1);
    averageFitness.getData().add(new XYChart.Data<Number, Number>(generation, avgFitness));
    maximumFitness.getData().add(new XYChart.Data<Number, Number>(generation, maxFitness));
    generation++;

}

点击按钮时会发生以下情况

public void twentyGenerationsPressed() {

    Task<Void> task = new Task<Void>() {
        @Override
        public Void call() throws InterruptedException {
        for (int i = 1; i <= 20; i++) {
            disableButtons();
            solver.nextGeneration();
            updateProgress(i, 20);
            executor.execute(() -> {
            addData(solver.getAverage(), solver.getCurrentBest().getFitness());
            });

        }

        return null;
        }
    };
    task.setOnSucceeded(e -> {
        updateLabels();
        enableButtons();
    });

    progressBar.progressProperty().bind(task.progressProperty());

    Thread th = new Thread(task);
    th.setDaemon(true);
    th.start();

}

2 个答案:

答案 0 :(得分:2)

根据经验,无论何时修改JavaFX对象的内容,都应该在JavaFX应用程序线程上执行。

替换它:

executor.execute(() -> {
    addData(solver.getAverage(), solver.getCurrentBest().getFitness());
});

有了这个:

final double avg = solver.getAverage();
final double best = solver.getCurrentBest().getFitness();
Platform.runLater(new Runnable() {
    @Override
    public void run() {
        addData(avg, best);
    }
});

答案 1 :(得分:0)

尝试更新Timeline动画中的图表。

   Timeline timeline = new Timeline();
   timeline.getKeyFrames().add(new KeyFrame(Duration.millis(100), evt -> addData(...)));
   timeline.setCycleCount(20);

   button.setOnAction(e -> timeline.play());

时间轴有好处,您可以指定更新之间的持续时间,我在您的代码中看不到