how to stop “JavaFX Application Thread”

后端 未结 4 1683
暖寄归人
暖寄归人 2020-12-15 06:31

I have a simple console application which sometimes need to perform graphics operations, for those I\'m using JavaFx framework (there are some functions that I need like the

4条回答
  •  攒了一身酷
    2020-12-15 06:59

    Fix:

    I was able to fix this problem by calling com.sun.javafx.application.PlatformImpl.tkExit() immediately before Platform.exit(). I don't really understand the JavaFX source that well, but it seems to be working; YMMV.

    Update: Doing this in Java 8 will produce a warning, you can just turn the warning off with @SuppressWarnings("restriction"). It shouldn't be a problem.

    Explanation:

    I figured this out by digging through the source code; JFXPanel has this little snippet (this is from JavaFX 2.2.25)

    finishListener = new PlatformImpl.FinishListener() {
      public void idle(boolean paramAnonymousBoolean) {
        if (!JFXPanel.firstPanelShown) {
          return;
        }
        PlatformImpl.removeListener(JFXPanel.finishListener);
        JFXPanel.access$102(null);
        if (paramAnonymousBoolean)
          Platform.exit();
      }
    
      public void exitCalled()
      {
      }
    

    The problem is, if you are using only a little bit of JavaFX in your application, then the idle(boolean) method never does anything (because firstPanelShown == false), which prevents the listener from getting removed, which prevents the JavaFX Toolkit from shutting down... which means you have to shut it down manually.

提交回复
热议问题