2

I have a javafx 8 app that has multiple active thread when I want to close it, this code shows 8 active threads :

    ThreadGroup group = Thread.currentThread().getThreadGroup();
    LOG.debug("Number of active threads  = " + group.activeCount());

Using Platform.exit(); and Platform.setImplicitExit(true); doesn't get me a clean exit.

I tried killing all threads before or after calling Platform.exit(); but without success.

Even setting threads as deamons before interrupting didn't solve it.

        while (group != null) {
        group.setDaemon(true);
        group.interrupt();
        group = Thread.currentThread().getThreadGroup();
    }

Calling System.exit(0) is not an option here as other apps can be launched from within other apps or standalone.

The error shown before exit is Not on FX application thread; currentThread = JavaFX Application Thread.

Edit 1: the stack trace of the error :

    ERROR SimpleUncaughtExceptionHandler.java uncaughtException 34     [JavaFX Application Thread] Uncaught exception
java.lang.IllegalStateException: Not on FX application thread; currentThread = JavaFX Application Thread
    at com.sun.javafx.tk.Toolkit.checkFxUserThread(Toolkit.java:236) ~[jfxrt.jar:?]
    at com.sun.javafx.tk.quantum.QuantumToolkit.checkFxUserThread(QuantumToolkit.java:423) ~[jfxrt.jar:?]
    at javafx.stage.Window.setShowing(Window.java:921) ~[jfxrt.jar:?]
    at javafx.stage.Window.hide(Window.java:947) ~[jfxrt.jar:?]
    at com.sun.javafx.stage.WindowPeerListener.closed(WindowPeerListener.java:100) ~[jfxrt.jar:?]
    at com.sun.javafx.tk.quantum.GlassWindowEventHandler.run(GlassWindowEventHandler.java:118) ~[jfxrt.jar:?]
    at com.sun.javafx.tk.quantum.GlassWindowEventHandler.run(GlassWindowEventHandler.java:40) ~[jfxrt.jar:?]
    at java.security.AccessController.doPrivileged(Native Method) ~[?:1.8.0_121]
    at com.sun.javafx.tk.quantum.GlassWindowEventHandler.lambda$handleWindowEvent$423(GlassWindowEventHandler.java:150) ~[jfxrt.jar:?]
    at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389) ~[jfxrt.jar:?]
    at com.sun.javafx.tk.quantum.GlassWindowEventHandler.handleWindowEvent(GlassWindowEventHandler.java:148) ~[jfxrt.jar:?]
    at com.sun.glass.ui.Window.handleWindowEvent(Window.java:1266) ~[jfxrt.jar:?]
    at com.sun.glass.ui.Window.notifyDestroy(Window.java:1183) ~[jfxrt.jar:?]

Edit 2 : List of active threads:

DEBUG [JavaFX Application Thread]        isDeamon : false; name : main
DEBUG [JavaFX Application Thread]        isDeamon : true; name : QuantumRenderer-0
DEBUG [JavaFX Application Thread]        isDeamon : false; name : JavaFX-Launcher
DEBUG [JavaFX Application Thread]        isDeamon : true; name : URL-Loader-2
DEBUG [JavaFX Application Thread]        isDeamon : true; name : URL-Loader-1
DEBUG [JavaFX Application Thread]        isDeamon : true; name : JavaFX Application Thread
DEBUG [JavaFX Application Thread]        isDeamon : true; name : Java2D Queue Flusher
DEBUG [JavaFX Application Thread]        isDeamon : true; name : Prism Font Disposer
DEBUG [JavaFX Application Thread]        isDeamon : true; name : Reference Handler
DEBUG [JavaFX Application Thread]        isDeamon : true; name : Thread-6
DEBUG [JavaFX Application Thread]        isDeamon : false; name : AWT-Shutdown
DEBUG [JavaFX Application Thread]        isDeamon : true; name : Java2D Disposer
DEBUG [JavaFX Application Thread]        isDeamon : true; name : Signal Dispatcher
DEBUG [JavaFX Application Thread]        isDeamon : true; name : Disposer
DEBUG [JavaFX Application Thread]        isDeamon : true; name : Finalizer
DEBUG [JavaFX Application Thread]        isDeamon : false; name : AWT-EventQueue-0
DEBUG [JavaFX Application Thread]        isDeamon : true; name : Thread-5
DEBUG [JavaFX Application Thread]        isDeamon : true; name : Timer-0
josephino
  • 360
  • 4
  • 21
  • What does "doesn't get me a clean exit" mean? – DVarga Feb 01 '17 at 11:50
  • Exiting without the exception 'not on FX application thread ..' – josephino Feb 01 '17 at 12:32
  • 4
    @GltknBtn Don't use `thread.stop()`, **ever**. It is [inherently unsafe](http://docs.oracle.com/javase/8/docs/api/java/lang/Thread.html#stop--) and has been deprecated. – James_D Feb 01 '17 at 14:07
  • 1
    If you are getting an exception, figure out why. This exception means you are updating the UI from a background thread somewhere. Check the [stack trace](http://stackoverflow.com/q/3988788/2775450), which will tell you where that is happening. – James_D Feb 01 '17 at 14:09
  • @James_D I'm not trying to update the UI from a background thread, I'm trying to quit the application, and I have multiple active threads running. – josephino Feb 01 '17 at 14:35
  • Post the stack trace in your question. – James_D Feb 01 '17 at 14:37
  • @James_D, I know it is unsafe, But is there any way to stop a running thread safely? Thanks your advice. – GltknBtn Feb 01 '17 at 14:42
  • @GltknBtn See the link in the documentation that I linked in the previous comment: [Why are Thread.stop ... deprecated](http://docs.oracle.com/javase/8/docs/technotes/guides/concurrency/threadPrimitiveDeprecation.html). – James_D Feb 01 '17 at 14:44
  • @James_D , I updated my question with the stack trace, I couldn't paste all of it as the same error is repeated. – josephino Feb 01 '17 at 14:50
  • OK, that looks very weird. You're not actually naming any of your own threads "JavaFX Application Thread", are you? Is there any of the stack trace that actually refers to code you have written? Is it possible to create a [MCVE] that reproduces this? – James_D Feb 01 '17 at 14:54
  • @James_D, thanks for the link. Pulled back the comment. – GltknBtn Feb 01 '17 at 15:00
  • @James_D maybe because I'm using Proguard and it obfuscates code. A minimal complete and verifiable example would be a javafx app with multiple threads active at the moment when Platform.exit() is called. I will try to produce one. – josephino Feb 01 '17 at 15:06
  • Perhaps first check if the error still occurs if you build it without Proguard? – James_D Feb 01 '17 at 15:09
  • @James_D , meanwhile I added the list of the active threads , see **Edit 2**. `AWT-Shutdown` , `AWT-EventQueue-0` , `JavaFX-Launcher` and `main` are not deamons. – josephino Feb 02 '17 at 11:42
  • Is there anything in the FX application's `stop` method? – RealSkeptic Feb 02 '17 at 11:52
  • @RealSkeptic yes , some cleanup methods that close connections and delete temp files. – josephino Feb 02 '17 at 12:38
  • You should check then that the cleanup methods don't do anything GUI-related like presenting a confirmation or save dialog etc. – RealSkeptic Feb 02 '17 at 14:11

1 Answers1

1

This problem and OSX System menubar not working in JavaFX have the same root cause which is the splash screen I'm using "java -splash:..", disabling it solves the problem.

As Splash Screen is an AWT feature and not a JavaFX feature ,it seems to be launched within AWT Event Thread and not JavaFX event Thread.

It looks like that the splash screen in JavaFX should be implemented from within the application as a JavaFX Pane.

Community
  • 1
  • 1
josephino
  • 360
  • 4
  • 21