how to stop “JavaFX Application Thread”

后端 未结 4 1677
暖寄归人
暖寄归人 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.

    0 讨论(0)
  • 2020-12-15 07:02

    I tried a lot of things on this as none of the above answers worked for me. Best thing for me was to just shutdown the whole JVM using

    System.exit(1);
    

    as my whole Application is just that one JavaFX application and therefor can be shut down, when closing the Stage. The 1 in my case is just a random int.

    0 讨论(0)
  • 2020-12-15 07:19

    That's a slippery situation, as (to my understanding) the purpose of the JavaFX thread is to take advantage of various hardware pipelines transparently. Might I suggest placing your JavaFX requests in a separate, referenced, project; and keep everything else, including your main method, in another? That's always worked for me.

    Basically, business logic and model go in one project, and view and control (generally JavaFX-based) go in the other. This allows for independent termination of the JavaFX thread. Hopefully that is applicable to what you are trying to do.

    0 讨论(0)
  • 2020-12-15 07:21

    Your main function does not belong to the JavaFx Application object and i think that your program never eneter application thread loop.

    It seems you should do:

    public class SOTestFX extends Application {
        public static void main(String[] args) {
            launch(args);
        }
        @Override
        public void start(Stage stage) throws Exception {
            // Do stuff here to show your stage or whatever you want;
            // This will be called on the JavaFX thread
        }
    }
    
    0 讨论(0)
提交回复
热议问题