由于OSX中的其他线程,Platform.exit()没有结束Javafx

时间:2017-02-01 11:21:14

标签: java multithreading macos javafx javafx-8

当我要关闭它时,我有一个javafx 8应用程序有多个活动线程, 此代码显示8个活动线程:

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

使用Platform.exit();Platform.setImplicitExit(true);并不能让我彻底退出。

我在调用Platform.exit();之前或之后尝试杀死所有线程,但没有成功。

即使在中断之前将线程设置为deamons也无法解决问题。

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

此处不能调用System.exit(0),因为其他应用可以在其他应用中启动,也可以单独启动。

退出前显示的错误为Not on FX application thread; currentThread = JavaFX Application Thread

编辑1:错误的堆栈跟踪:

    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:?]

编辑2:活动线程列表:

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

1 个答案:

答案 0 :(得分:1)

此问题和OSX System menubar not working in JavaFX具有相同的根本原因,即我正在使用“java -splash:..”的启动画面,禁用它可以解决问题。

由于Splash Screen是AWT功能而不是JavaFX功能,它似乎是在AWT事件线程中启动而不是JavaFX事件线程。

看起来splash screen中的JavaFX应该在应用程序中以JavaFX Pane的形式实现。