title: JavaFX Application Thread slows and then freezes
body: <p>I have a multi-threaded JavaFX application. I have a bunch of background threads that are calling this method to update the main UI from the application thread.</p>
  
  <pre><code>public void writeToActivityLog(String message){
  
      class ThreadedTask implements Runnable {
  
          private String message;
  
          public ThreadedTask(String message){
              this.message = message;
          }
  
          @Override
          public void run() {
              try{
                  //delete older text when there are more than 200 lines and append new text
  
                  String text = outputLog.getText();
                  String[] lines = text.split("\r\n");
                  String newText = "";
                  for(int i = 0; i &lt; lines.length; i++){
                      if(i &gt;= 200 || lines.length &lt; 200){
                          newText += lines[i];
                      }
                  }
                  outputLog.setText(newText);
                  outputLog.appendText(message + "\r\n");
  
                  //scroll to the bottom of the log
                  outputLog.setScrollTop(Double.MIN_VALUE);
  
              } catch(NullPointerException e){
                  e.printStackTrace();
              }
          }
      }
  
      ThreadedTask thread = new ThreadedTask(message);
      Platform.runLater(thread);
  
  }
  </code></pre>
  
  <p>At first, this method works fine. But a few seconds into the program, it begins to slow, and a few more seconds later the whole UI freezes and stops responding to user input (but it doesn't trigger the Windows "This program is not responding" dialogue). However, looking at application log files and my IDE console, the background threads are still executing and seem to be doing just fine.</p>
  
  <p>Is there some limit as to the number of <code>Platform.runLater()</code> requests that can be queued up? Or is there some way I might have a memory leak that is killing the main application thread but isn't doing anything to the background threads? I am still very new to multi-threaded programming, so I don't know what to think here.</p>
  
  <p>I also know that JavaFX has another concurrency tool called Services, but I can't find any explanation as to when and why I should be using them instead of <code>Platform.runLater()</code>. I have written other JavaFX apps and never had any issues using <code>Platform.runLater()</code>.</p>
  
  <p>Edit: </p>
  
  <p>Thanks a lot to the two answers below. The problem was nothing with JavaFX or threads  per se but just plain bad code. Here is the fixed code, for completion sake:</p>
  
  <pre><code>public void writeToActivityLog(String message){
      //Create a new thread to update the UI so that it doesn't freeze due to concurrency issues
      class ThreadedTask implements Runnable {
  
          private String message;
  
          public ThreadedTask(String message){
              this.message = message;
          }
  
          @Override
          public void run() {
              outputLog.setText(message);
  
              //scroll to the bottom of the log
              outputLog.setScrollTop(Double.MIN_VALUE);
          }
      }
  
      String wholeMessage = new StringBuilder().append(outputLog.getText()).append(message).toString();
      //modify the text of the output log so that it is 200 lines or less
      StringBuilder newText = new StringBuilder();
      String[] lines = wholeMessage.split("\r\n");
  
      for (int i=Math.max(0, lines.length - 200); i&lt;lines.length; i++) {
          newText.append(new StringBuilder().append(lines[i]).append("\r\n").toString());
      }
  
      ThreadedTask thread = new ThreadedTask(newText.toString());
      Platform.runLater(thread);
  }
  </code></pre>