title: Multithreading in JavaFX hangs the UI
body: <p>I have a simple JavaFX 2 app, with 2 buttons, saying Start and Stop. When the start button is clicked, I want to create a background thread which will do some processing and update the UI (e.g a progress bar) as it goes along. If the stop button is clicked, I want the thread to terminate.</p>
  
  <p>I've tried to do this using the <code>javafx.concurrent.Task</code> class which I gathered from the documentation would work fine for this. But whenever I click Start, the UI freezes/hangs rather than staying normal.</p>
  
  <p>Her's the code from the main <code>Myprogram extends Application</code> class for showing the buttons:</p>
  
  <pre><code>public void start(Stage primaryStage)
  {               
      final Button btn = new Button();
      btn.setText("Begin");
  
      //This is the thread, extending javafx.concurrent.Task :
      final MyProcessor handler = new MyProcessor();
      btn.setOnAction(new EventHandler&lt;ActionEvent&gt;()
      {
          public void handle(ActionEvent event)
          {                
             handler.run(); 
          }
      });
  
      Button stop = new Button();
      stop.setText("Stop");
      stop.setOnAction(new EventHandler&lt;ActionEvent&gt;()
          {
               public void handle(ActionEvent event)
               {
                  handler.cancel();
               }
          }
  
      );
      // Code for adding the UI controls to the stage here.
  }
  </code></pre>
  
  <p>Here's the code of <code>MyProcessor</code> class:</p>
  
  <pre><code>import javafx.concurrent.Task;
  public class MyProcessor extends Task
  {   
      @Override
      protected Integer call()
      {
          int i = 0;
          for (String symbol : feed.getSymbols() )
          {
              if ( isCancelled() )
              {
                  Logger.log("Stopping!");
                  return i;
              }
              i++;
              Logger.log("Doing # " + i);
              //Processing code here which takes 2-3 seconds per iteration to execute
              Logger.log("# " + i + ", DONE! ");            
          }
          return i;
      }
  }
  </code></pre>
  
  <p>Pretty simple, but the UI hangs whenever I click the Start button, though the console messages continue to get displayed (<code>Logger.log</code> simply does <code>System.out.println</code> )</p>
  
  <p>What am I doing wrong?</p>