title: JavaFX Chart Series ConcurrentModificationException
body: <p>I'm building a genetic algorithm on JavaFX and I want to have a chart that will update the average and the maximum fitness of every generation. 
  I have A button, that does 20 generations, updates the graph every iteration and also the progress bar.</p>
  
  <p>The code used to be simplier, without all those duplicares and references. These are just the ideas on how to avoid the exception. But nothing worked</p>
  
  <p>I mean, I don't even iterate or delete something. This is just about adding.</p>
  
  <p>I get this exception:</p>
  
  <pre><code>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)
  </code></pre>
  
  <p><strong>Here is the preparation</strong></p>
  
  <pre><code>public void prepareChart() {
      final NumberAxis xAxis = new NumberAxis();
      final NumberAxis yAxis = new NumberAxis();
      final AreaChart&lt;Number, Number&gt; graph = new AreaChart&lt;Number, Number&gt;(xAxis, yAxis);
      graph.setCreateSymbols(false);
      graph.setTitle("Fitness - Generations relation");
      graph.getXAxis().setLabel("Generations");
      graph.getYAxis().setLabel("Fitness");
  
      final Series&lt;Number, Number&gt; averageFitness = new XYChart.Series&lt;Number, Number&gt;();
      averageFitness.setName("Average fitness");
  
      final Series&lt;Number, Number&gt; maximumFitness = new XYChart.Series&lt;Number, Number&gt;();
      maximumFitness.setName("Maximum fitness");
      graph.getData().addAll(averageFitness, maximumFitness);
      graphBox.getChildren().add(graph);
      graphRef = graph;
  }
  </code></pre>
  
  <p><strong>Here is how I add data</strong></p>
  
  <pre><code>public void addData(double avgFitness, double maxFitness) {
      final Series&lt;Number, Number&gt; averageFitness = graphRef.getData().get(0);
      final Series&lt;Number, Number&gt; maximumFitness = graphRef.getData().get(1);
      averageFitness.getData().add(new XYChart.Data&lt;Number, Number&gt;(generation, avgFitness));
      maximumFitness.getData().add(new XYChart.Data&lt;Number, Number&gt;(generation, maxFitness));
      generation++;
  
  }
  </code></pre>
  
  <p><strong>Here is what happens when the button is clicked</strong></p>
  
  <pre><code>public void twentyGenerationsPressed() {
  
      Task&lt;Void&gt; task = new Task&lt;Void&gt;() {
          @Override
          public Void call() throws InterruptedException {
          for (int i = 1; i &lt;= 20; i++) {
              disableButtons();
              solver.nextGeneration();
              updateProgress(i, 20);
              executor.execute(() -&gt; {
              addData(solver.getAverage(), solver.getCurrentBest().getFitness());
              });
  
          }
  
          return null;
          }
      };
      task.setOnSucceeded(e -&gt; {
          updateLabels();
          enableButtons();
      });
  
      progressBar.progressProperty().bind(task.progressProperty());
  
      Thread th = new Thread(task);
      th.setDaemon(true);
      th.start();
  
  }
  </code></pre>