title: How to end JavaFX threads and terminating them when called by Platform.runLater()?
body: <p>I have a program which is not in JavaFX application thread, and I want to call some JavaFX application in it in order to do something for me.
  Here is my code:</p>
  
  <pre><code>public void extractImages(Document document) {
          ImageExtractor_HTML_Data.getInstance().documentId = document.getId();
          if (!ImageExtractor_HTML_Data.getInstance().started)
              new JFXPanel();
          ImageExtractor_HTML_Data.getInstance().started = true;
          Platform.runLater(new Runnable() {
              ImageExtractor_HTML_Browser browser = new ImageExtractor_HTML_Browser();
  
              @Override
              public void run() {
                  browser.start(new Stage());
              }
          });
  
          synchronized (ImageExtractor_HTML_Data.getInstance()) {
              try {
                  ImageExtractor_HTML_Data.getInstance().wait();
              } catch (InterruptedException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              }
          }
      }
  </code></pre>
  
  <p>And my Browser class is:</p>
  
  <pre><code>public class ImageExtractor_HTML_Browser extends Application {
      private Scene scene;
  
      @Override
      public void start(Stage stage) {
  
          /* Some code */
  
          stage.setTitle("Web View");
          Browser browser = new Browser(htmlFile, stage);
          scene = new Scene(browser, 750, 500, Color.web("#666970"));
          stage.setScene(scene);
          stage.show();
          browser.imageExtractor(new ImageHandler_DB_HTML(document));
      }
  </code></pre>
  
  <p>And my imageExtractor method is:</p>
  
  <pre><code>public void imageExtractor(final ImageHandler handler) {
          webEngine.getLoadWorker().stateProperty()
                  .addListener(new ChangeListener&lt;Worker.State&gt;() {
                      @Override
                      public void changed(
                              @SuppressWarnings("rawtypes") ObservableValue observableValue,
                              Worker.State state, Worker.State newState) {
                          if (newState.equals(Worker.State.SUCCEEDED)) {
                              /* Some code which extract some data */
                              synchronized (ImageExtractor_HTML_Data
                                      .getInstance()) {
                                  ImageExtractor_HTML_Data.getInstance().notify();
                              }
                              stage.close();
                              Platform.exit();
                              return;
                          }
                      }
                  });
      }
  </code></pre>
  
  <p>In imageExtractor method I want to wait until my HTML file load completly and then I want to do something, so I use WebEngine.getLoadWorker.etc.
  My problem is that after run of this code, my JavaFX threads didn't finish so my program didn't end.
  Although, I don't want to use System.exit(0) because my program runs in the middle of a project which runs via tomcat and if I use System.exit(0), whole project comes down which is not good at all!</p>
  
  <p>Can anyone help me?</p>