title: How to terminate a scheduled thread when JavaFX Runtime exits?
body: <p>I'm looking over similar examples to this problem.  We have a JavaFX app which runs some GUI updates via thread running from: ScheduledExecutorService::<strong>scheduleAtFixedRate</strong>.</p>
  
  <p>This is similar to a couple of other questions.  The two I recognised as most like my situation are these:</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/22657192/javafx-task-threads-not-terminating">JavaFX Task threads not terminating</a></li>
  <li><a href="https://stackoverflow.com/questions/15808063/how-to-stop-javafx-application-thread">how to stop &quot;JavaFX Application Thread&quot;</a></li>
  </ul>
  
  <p>The question I need to resolve, however, is about the next step.  My target is for an embedded application and there's no opportunity to manually kill the JVM-task, or the other easy answers, etc.  I'm afraid a reboot is reserved for something critically-serious.</p>
  
  <p>We need to ensure that all threads are closed off in an orderly way.  What I'm looking for is some kind of call back or event that lets me register a <strong>clean-up</strong> routine to close-down my <em>stuff</em>?</p>
  
  <p>I was thinking that there ought to be '<em>something</em>' in the base class, JavaFX <a href="http://docs.oracle.com/javafx/2/api/javafx/application/Application.html" rel="nofollow noreferrer">javafx.application.Application</a> to do the deed.  </p>
  
  <ul>
  <li><a href="http://docs.oracle.com/javafx/2/api/javafx/application/Application.html" rel="nofollow noreferrer">http://docs.oracle.com/javafx/2/api/javafx/application/Application.html</a></li>
  </ul>
  
  <p>Is the Stop method something I might use or can I register to be called when there is a stop from my FXMLController?</p>
  
  <p>At present when I run my JavaFX app from Netbeans, the JVM process persists.  This stops any further build scripts and locks the JAR file.  Netbeans gives you an option to kill the task.  The true solution means that the application/JVM closes-down orderly and neatly.</p>
  
  <p>(<strong><em>update</em></strong>) ... I looked into the javafx.Application class that you use to launch the JavaFX app.  I implemented a Stop() method.  Here I make sure that I've called <strong>Platform</strong>.<strong>exit</strong>() ...</p>
  
  <pre><code>/////
  //  @see
  //    -- http://docs.oracle.com/javafx/2/api/javafx/application/Application.html#stop%28%29
  //
  public void stop()
  {
      Platform.exit();
  }
  </code></pre>
  
  <p>This doesn't cure the problem when running from NetBeans.  Sometimes you need to click the stop [X] button two times, but the process does stop when you use the kill button.  If you are interested in progress this is reported as bug: <a href="https://netbeans.org/bugzilla/show_bug.cgi?id=245284" rel="nofollow noreferrer">[Bug 245284]</a>, there's a small clock example to demonstrate the problem.  When you close the window, the NetBeans process running panel is still '<em>running</em>'.  You can't build because the JAR file is locked.  At least we know to manually kill the development program.  </p>
  
  <ul>
  <li>Suggestions welcome . . .</li>
  </ul>