1

I learned that the stop method of the Application class of the JavaFX application is called when it is about to terminate. Does it mean that all the threads of my application continues to execute until the stop method has not returned?

So the termination of my application’s threads start meanwhile the execution of the stop method, or the termination of the threads start only after the stop method finished execution?

Patrik Nusszer
  • 460
  • 3
  • 13
  • what do you mean every thing else ? – Ahmed Emad Sep 08 '18 at 21:12
  • It isn't clear what you're asking, or rather - it feels a bit like a [XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). That said, please read the [documentation of `Thread`](https://docs.oracle.com/javase/8/docs/api/java/lang/Thread.html) as well as [`Platform.exit`](https://docs.oracle.com/javase/8/javafx/api/javafx/application/Platform.html#exit--). The short version is that if you've called `Platform.exit` then the JVM won't exit until all other non-daemon threads have finished. – Itai Sep 12 '18 at 11:08

2 Answers2

0

From docs here.

This method is called on the JavaFX Application Thread.

So if you have other threads performing their own tasks. Its your responsibility to handle them.

Rengas
  • 593
  • 1
  • 6
  • 25
  • I hope i understand your comment. JavaFx thread doesn't manage your own threads or send signal to other thread. The stop method is there to wrap things up before exiting the application e.g. closing resources that was opened during application startup. You can also choose to send signal to other threads or whatever you want. – Rengas Sep 11 '18 at 22:09
  • When the stop method is called I would do the signaling myself. But do I have time to manage my threads until I do not let the stop method return? – Patrik Nusszer Sep 12 '18 at 23:30
  • There are number of ways you can wait till other threads stopped before return as suggested here https://stackoverflow.com/questions/702415/how-to-know-if-other-threads-have-finished – Rengas Sep 17 '18 at 01:21
0

When you call close() on your primaryStage JavaFX calls the handler assigned to setOnCloseRequest() if you have override it before it will execute your code then closes the main Thread
If you have any other threads that are still running you have to close it yourself unless they are daemon threads as they close with the app anyway or call System.exit(0) your app will force close all the threads opened by your app
If you want I can provide an example that demonstrate my words

Ahmed Emad
  • 674
  • 6
  • 19
  • JavaFX doesn't call `setOnCloseRequest`, but rather calls the handler last provided by a call to `setOnCloseRequest`. Furthermore, it will almost never be necessary to actually override that method. Lastly, the part about other running threads is only true for non-daemon threads. Daemon threads are automatically stopped by the JVM when the last non-daemon thread stops (which in JavaFX applications will usually only be the JavaFX thread). – Itai Sep 12 '18 at 11:13
  • You are totally right I forgot to mention about the daemon threads i will edit my answer but you can override that method to close some threads or notify the user that the app is gonna close if he/she wanna change mind – Ahmed Emad Sep 12 '18 at 11:35