JavaFX-Launcher 中的循环阻止 JavaFX 应用程序线程启动
Loop in JavaFX-Launcher preventing JavaFX application thread from starting
如果您在 JavaFX-Launcher Thread
中设置无限循环,您的应用程序将永远不会启动。据我所知,它在不同的 Thread
上运行,那么为什么会发生这种情况?
只需创建一个新项目并覆盖 init()
并添加一个永久循环。
// Mine is not forever
boolean go = true;
@Override
public void init() throws Exception {
// TODO Auto-generated method stub
super.init(); // 'while' comes after this line
while(go){} // Comment this guy and everything is ayt
}
@Override
public void stop() throws Exception {
// TODO Auto-generated method stub
go = false;
super.stop();
}
这是否意味着 JavaFX-Launcher Thread
应该在您的应用程序 UI 可以启动之前退出 -(即 javaFxApplication 线程),或者我遗漏了什么?
如果 JavaFX-Launcher
应该是启动应用程序的替代方案 needs 在应用程序启动之前,为什么它不能并发,因为如果我希望它在successions 我会把我的代码放在 super.init()
方法之前吗?
我以前没有特别使用 javafx 的经验,但是当发生这种情况时,这是一个明显的迹象,表明您正在 运行 典型的事件驱动系统中,它有一条消息循环到 运行。
您的 init()
方法可能已被此消息循环调用,消息循环期待您的 init 方法 return 以便它可以继续获取和发送更多消息,以便系统可以运行.
你需要将你需要做的工作卸载到另一个线程,或者你需要找到一些方法将它分解成片段,这些片段将在某个计时器事件或某个空闲事件上零碎地执行,无论您的环境支持什么。
直接解释正在发生的事情
Application start 方法的文档明确说明了正在发生的事情:
The start method is called after the init method has returned, and after the system is ready for the application to begin running.
因此,如果您的 运行 init 方法中的无限循环从未 returns,您的应用程序将永远不会启动。
有关应用程序生命周期的更多文档在 Application javadoc 中。
一些旁白、猜测和可能的相关信息
以下信息可能与您手头的问题相关,也可能不相关。
我相信 init 方法背后的想法是您可以将逻辑放在 init 方法中,该方法可以在 JavaFX 系统本身初始化时执行。 JavaFX 系统的初始化需要一些时间,但在现代处理系统上,我不希望这是很长的时间(例如不到一秒)。
我见过的大多数 JavaFX 应用程序都没有太多地使用 init 方法。
如果您的初始化时间很长并且您希望您的应用程序在初始化完成之前快速启动,您将需要一些明确的逻辑来处理它。
例如,创建一个 JavaFX Task in your init method that runs on it's own thread. Perform your initialization there. In your start method display a UI immediately, with some limited functionality. Once your initialization is fully complete (which can be known by a listener on the status of your initialization Task), then enable the fully functional UI that depends on the data from the initialization being fully available. Although it is not quite the same (as it is running some of the Task logic from start rather than init), a very similar example of this approach is in this gist sample, which "Displays a JavaFX splash page for an intensive startup task with progress monitoring".
JavaFX 确实有一个 Preloader that provides an in-built framework for handling lengthy initialization, however I haven't seen that widely used. The Preloader itself is targeted mainly at Java embedded in a web page via a plugin or Java Web Start. These are not technologies used much in combination with JavaFX. Most JavaFX applications are standalone applications not relying on web page plugins or web start for their execution). You could use the Preloader framework for initializing your application (even if it works in a standalone mode), but probably it is simpler just to use a Task for this purpose. For further information on Preloader usage, you can refer to: How to use javaFX Preloader with stand-alone application in Eclipse? 的概念(请注意,这个问题的答案并不特定于 Eclipse,尽管问题是)。
如果您在 JavaFX-Launcher Thread
中设置无限循环,您的应用程序将永远不会启动。据我所知,它在不同的 Thread
上运行,那么为什么会发生这种情况?
只需创建一个新项目并覆盖 init()
并添加一个永久循环。
// Mine is not forever
boolean go = true;
@Override
public void init() throws Exception {
// TODO Auto-generated method stub
super.init(); // 'while' comes after this line
while(go){} // Comment this guy and everything is ayt
}
@Override
public void stop() throws Exception {
// TODO Auto-generated method stub
go = false;
super.stop();
}
这是否意味着 JavaFX-Launcher Thread
应该在您的应用程序 UI 可以启动之前退出 -(即 javaFxApplication 线程),或者我遗漏了什么?
如果 JavaFX-Launcher
应该是启动应用程序的替代方案 needs 在应用程序启动之前,为什么它不能并发,因为如果我希望它在successions 我会把我的代码放在 super.init()
方法之前吗?
我以前没有特别使用 javafx 的经验,但是当发生这种情况时,这是一个明显的迹象,表明您正在 运行 典型的事件驱动系统中,它有一条消息循环到 运行。
您的 init()
方法可能已被此消息循环调用,消息循环期待您的 init 方法 return 以便它可以继续获取和发送更多消息,以便系统可以运行.
你需要将你需要做的工作卸载到另一个线程,或者你需要找到一些方法将它分解成片段,这些片段将在某个计时器事件或某个空闲事件上零碎地执行,无论您的环境支持什么。
直接解释正在发生的事情
Application start 方法的文档明确说明了正在发生的事情:
The start method is called after the init method has returned, and after the system is ready for the application to begin running.
因此,如果您的 运行 init 方法中的无限循环从未 returns,您的应用程序将永远不会启动。
有关应用程序生命周期的更多文档在 Application javadoc 中。
一些旁白、猜测和可能的相关信息
以下信息可能与您手头的问题相关,也可能不相关。
我相信 init 方法背后的想法是您可以将逻辑放在 init 方法中,该方法可以在 JavaFX 系统本身初始化时执行。 JavaFX 系统的初始化需要一些时间,但在现代处理系统上,我不希望这是很长的时间(例如不到一秒)。
我见过的大多数 JavaFX 应用程序都没有太多地使用 init 方法。
如果您的初始化时间很长并且您希望您的应用程序在初始化完成之前快速启动,您将需要一些明确的逻辑来处理它。
例如,创建一个 JavaFX Task in your init method that runs on it's own thread. Perform your initialization there. In your start method display a UI immediately, with some limited functionality. Once your initialization is fully complete (which can be known by a listener on the status of your initialization Task), then enable the fully functional UI that depends on the data from the initialization being fully available. Although it is not quite the same (as it is running some of the Task logic from start rather than init), a very similar example of this approach is in this gist sample, which "Displays a JavaFX splash page for an intensive startup task with progress monitoring".
JavaFX 确实有一个 Preloader that provides an in-built framework for handling lengthy initialization, however I haven't seen that widely used. The Preloader itself is targeted mainly at Java embedded in a web page via a plugin or Java Web Start. These are not technologies used much in combination with JavaFX. Most JavaFX applications are standalone applications not relying on web page plugins or web start for their execution). You could use the Preloader framework for initializing your application (even if it works in a standalone mode), but probably it is simpler just to use a Task for this purpose. For further information on Preloader usage, you can refer to: How to use javaFX Preloader with stand-alone application in Eclipse? 的概念(请注意,这个问题的答案并不特定于 Eclipse,尽管问题是)。