title: Creating and stopping threads in JavaFX
body: <p>I have a task to work with threads and JavaFX library. A separate thread 1 need to be started by pressing a button. The thread is supposed to fulfill a list with Integer values (100 - 150) with a small interval (e.g. 5 ms).</p>
  
  <p>And after values are generated, it should stop. A new thread 2 must start and fill a ViewList with generated values. </p>
  
  <p>But I get an exception each time when a new value is added to my list by thread 1:    </p>
  
  <pre><code>Exception in thread "Thread-4" java.lang.IllegalStateException: Not on FX application thread; currentThread = Thread-4
      at com.sun.javafx.tk.Toolkit.checkFxUserThread(Toolkit.java:204)
      at com.sun.javafx.tk.quantum.QuantumToolkit.checkFxUserThread(QuantumToolkit.java:364)
      at javafx.scene.Scene.addToDirtyList(Scene.java:485)
      at javafx.scene.Node.addToSceneDirtyList(Node.java:424)
      at javafx.scene.Node.impl_markDirty(Node.java:415)
  </code></pre>
  
  <p>I tried to use <code>Platform.runLater()</code> in button event listener instead of creating a new Thread, but the program stops responding in that case. </p>
  
  <p>Could anyone please help me, how to fill a collection with values in a separate thread and how to start a second one to update ViewList element after the first thread finishes? </p>
  
  <p>Here is my class:</p>
  
  <pre><code>int itemName = 100;
  Button btnOne = new Button("Button one");
  Label label = new Label("Press the button to start");
  
  ObservableList&lt;String&gt; listOptions = FXCollections.observableArrayList();
  ListView&lt;String&gt; list;
  
  @Override
  public void start(Stage primaryStage) throws Exception{
      primaryStage.setTitle("Hurry");
  
      list = new ListView&lt;&gt;(listOptions);
      list.setPrefSize(120, 120);
  
      MultipleSelectionModel&lt;String&gt; lvSelModel =
              list.getSelectionModel();
  
      FlowPane rootNode = new FlowPane(10, 10);
      rootNode.setAlignment(Pos.CENTER);
  
      Scene myScene = new Scene(rootNode, 180, 200);
      primaryStage.setScene(myScene);
  
      lvSelModel.selectedItemProperty().addListener(
          (changed, oldValue, newValue) -&gt; {
          label.setText("List option selected: " + newValue);
      });
  
      rootNode.getChildren().addAll(btnOne, list, label);
      primaryStage.show();
  
      Task&lt;Integer&gt; threadOne = new Task&lt;Integer&gt;(){
          @Override
          protected Integer call() throws Exception{
  
            while(itemName &lt; 130){
                  final int finalValue = itemName++;
                  listOptions.add(String.valueOf(itemName));
                  Platform.runLater(
                  () -&gt;  label.setText("Generating: " + finalValue));
                  Thread.sleep(100);
              }
              label.setText("Thread 1 finished");
              return itemName;
          }
      };
  
      btnOne.setOnAction( 
          ae -&gt; {
          Thread th = new Thread(threadOne);
          th.setDaemon(true);
          th.start();
      });
  
  }
  
  public static void main(String[] args) {
      launch(args);
  }}`   
  </code></pre>
  
  <p>The result program should be like this:</p>
  
  <p><a href="https://i.stack.imgur.com/3Sg56.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/3Sg56.png" alt=""></a></p>
  
  <p>Thanks everyone for your time!</p>