在旁边:从理论上讲,我想您可以执行以下操作:public class MainClass { public static void main(String[] args) { new Thread(() -> Application.launch(MyAppStartClass.class, args)).start(); // do "background thread" work here in the (now free) main thread }}但是,由于该惯用语与通常的设置相去甚远,因此我不建议您这样做.特别要注意的是,您不应该直接在Application子类中拥有这样的主要方法,因为(我认为)Oracle JRE(或知道如何运行JavaFX Applications的环境)可能会忽略该主要方法而只是启动您的开始方法,如本文顶部所述.I'm trying to run a program using JavaFX. If I was using Swing, I would have one class started by the main method, and have it build the GUI class. That would give me 2 threads, normal thread for an application, and the EventQueue. That would prevent blocking the UI work.So, I tried to have the class created in the static main method construct the Application class, and then launch it. I got a RuntimeException because the program calling the launch method was not a subclass of Application.Is there a way to have separation of threads, or does everything have to work inside a the thread given to an Application class? 解决方案 In JavaFX, I'm guessing in order to prevent the very common threading errors that were present in many Swing applications, the startup process is constrained so that (more or less) the only way to do things forces you to execute the UI code on the FX Application Thread:public class MyAppStartClass extends Application { @Override public void start(Stage primaryStage) { // this method will be executed on the FX Application Thread // load UI and display it here.... }}In an Oracle JRE, executing java MyAppStartClass will (in contrast to a regular Java application), cause an instance of MyAppStartClass to be created, the FX Application Thread to be started, and the start method of the created instance to be executed on the FX Application Thread. (There is a bit more to it than that, but that's the basic gist.)If you want to support environments that aren't aware of how to execute a JavaFX Application (including many IDEs), you can add a main method that forces this to happen, simply by calling the static Application.launch() method:public class MyAppStartClass extends Application { @Override public void start(Stage primaryStage) { // this method will be executed on the FX Application Thread // load UI and display it here.... } // to support non-JavaFX-aware environments: public static void main(String[] args) { launch(args); }}Note there's an overloaded form of launch, where you can specify an Application subclass, so you could have a different main class (the use cases for this are very few):public class MainClass { public static void main(String[] args) { Application.launch(MyAppStartClass.class, args); }}There are two important features of the launch method to be aware of:It can only be called once per JVM lifetime. Calling it a second time will throw an exception.It will block until the JavaFX platform exits.If you want to have a thread doing work besides the FX Application Thread, the simplest way is to start it directly from the start method:public class MyAppStartClass extends Application { @Override public void start(Stage primaryStage) { // start a background thread to do background stuff: new Thread(() -> { // background work... }).start(); // UI work... }}If you compare this to the standard startup for a Swing application:public class MySwingApp { public static void main(String[] args) { SwingUtilities.invokeLater(() -> { // UI work... }); // background work... }}the process is kind of inverted compared to Swing. In Swing you (are supposed to) explicitly have the startup method (main) specify that the UI is run on the AWT event dispatch thread, and you can do other stuff in the "current" thread in which main is executed. In JavaFX the startup method (start) is executed on the UI thread, and if you want to do stuff on another thread you explicitly launch one.Other rules are pretty much the same: UI elements that are part of the scene graph can only be modified on the UI thread, etc. Note JavaFX has a specific concurrency API for managing tasks in a background thread and scheduling UI updates on the FX Application Thread.Aside:In theory I suppose you could do something like:public class MainClass { public static void main(String[] args) { new Thread(() -> Application.launch(MyAppStartClass.class, args)).start(); // do "background thread" work here in the (now free) main thread }}But since that idiom is far from the usual setup, I would not recommend it. Note especially you should not have a main method like this directly in the Application subclass, as (I think) an Oracle JRE (or an environment that knows how to run JavaFX Applications) might ignore the main method and just boot up your start method as described at the top of this post. 这篇关于如何为我的代码启动一个线程,为JavaFX Application启动一个线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!