title: JavaFX Multithreading
body: <p>I am facing one problem in my JavaFx application.</p>
  
  <p><strong>Preface</strong>: I don't want to be specific to my application only but want to be generalized so that people like me will get an idea on similar situation.</p>
  
  <p><strong>Background</strong>: Implementing a Javafx application using fxml file and multi-threading concept. </p>
  
  <p><strong>Summary</strong>: I tried make an application which basically uses multi threading to do some task, and once multi- threading is completed, it will sequentially move to another task. While doing the multi-threading the Main GUI freezes. </p>
  
  <p><strong>What I did</strong>, </p>
  
  <pre><code>//This is Main class
  
  Parent Root  -&gt; defined on top
  public Parent createContent(){
   try{
    root =  FXMLLoader.load(getClass().getResource("Layout.fxml"));
    }catch { .....}
   }
  
   public void start(){
      stage.setScene(new Scene(createContent()));
      Task&lt;Void&gt; task = new Task&lt;Void&gt;() {
              @Override
              public Void call() throws Exception {
                  Thread.sleep(1000);
                  return null ;
              }
          };
        task.setOnSucceeded(event-&gt;{
              stage.show();
          });
          new Thread(task).run();
    }
    public static void main(String[] args) {
          launch(args);
      }
  // This is inside the controller on button click event
  @FXML
  private void start(ActionEvent event) { &lt;---- This one is button click event
     Thread t1 = new Thread(new Mul());
      t1.start();
      Thread t2 = new Thread (new Mul());
      t2.start();
  }
  
   // Finally 
  public class Mul implements Runnable {
    public void type() {
      for (int a = 0; a &lt; 200000; a++) {
          System.out.println( Thread.currentThread().getName()+ " says " + a);
      }
     }
      @Override
     public void run() {
       type();
     }
  
  }
  </code></pre>
  
  <p>Now, here is the outcome.
  If I just start the threads from the controller then My main application does not freezes while the thread are running in background. However, since the application runs in a sequence, i.e the next step only works when the threads complete their work</p>
  
  <p>I can use t1.join() and t2.join() , but doing so will freezes my main application(Main application here is the main GUI) and I cannot proceed with it until the threads are completed. </p>
  
  <p>What could be the optimal solution, so that I can use multi threading without blocking the main application  or <strong>What am I doing wrong here</strong>? (<strong>info</strong>, <em>I came to this solution by following up different suggestions from Stack overflow and google</em>) </p>