有没有办法快速编译/加载fxml文件只有一次,而不是每次重启应用程序? [英] Is there a way to compile / load fxml files faster and only one time, and not at every restart of an application?

查看:142
本文介绍了有没有办法快速编译/加载fxml文件只有一次,而不是每次重启应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个程序在执行时加载许多fxml文件。应用程序将在短时间内完成,加载应用程序只需要很长时间。

I have a program which loads many fxml files when executed. The application will be finished in a short time, and loading the application just takes too long.

有许多fxml文件(20+)并且所有这些fxml文件都已加载使用Java代码。应用程序已完成并可以使用,但每次执行程序时都会加载所有文件。 fxml文件只能编译一次,因为完成后它们不会被更改吗?

There are many fxml files (20+) and all these fxml files are loaded with Java code. There will be a point that the application is finished and ready for use, but all files will be loaded with every execution of the program. Can the fxml files only be compiled once, because they won't be changed when finished?

java代码当然会被编译一次,它只是fxml文件。该应用程序现在需要25秒才能启动,加载fxml需要14秒。

The java code will of course be compiled once, it's only the fxml files. The application now takes 25 seconds to start, with 14 seconds loading the fxml.

有没有办法让这一切更快?

Is there a way to make all this faster?

编辑#1:

是否有免费提供的工具可以执行应用程序(Java ) 快多了?或者执行时间是否仅取决于程序的编写方式?

Are there any tools that are provided for free and that make the execution of applications (Java) much faster? Or is the execution time merely dependent on the way the program is written?

哪些设计模式可以帮助缩短应用程序的执行时间?

Which design patterns could help fastening up the execution time of your application?

编辑#2:

以下代码将简要解释我的问题:

The following code will explain my problem in a nutshell:

package main;

.......
.......

public class MainClass {

    ....
    ....

    List<InformationController> controllerList;

    public mainClass(List<InformationControllers> controllerList) {
        this.controllerList = otherClass.getControllerList();
        loadFXMLFiles();
    }

    public void loadFXMLFiles() {
        for(InformationController controller : controllerList) {
            controller.loadFXML();
        }
        doOtherStuff();
    }

    ....

}

所有这些InformationControllers也加载了其他fxml文件,因此它实际上是一个加载的fxml文件树。这是在应用程序的每次启动时加载的,所以有没有办法只加载一次?

All those InformationControllers have their loading of other fxml files too, so it's actually a tree of fxml files that is loaded. This is loaded at every start of the application, so is there a way to do this loading only once maybe?

推荐答案

关于如何提高FXML性能

我将制作这个答案社区维基,如果有其他人有进一步的想法,请编辑并添加它们。

I'll make this answer community wiki, if anybody else has further ideas, please edit it and add them.


  1. 一个人不应该使用静态 FXMLLoader.load() 但应改为创建FXMLLoader实例并使用实例重用该实例 loader.load() 方法。原因是FXML-Loader缓存,例如,类查找,因此加载下一个FXML文件的速度更快。

  2. 使用工具将FXML编译为Java,例如 Tom Schindl的FXML编译器 NetBeans FXML到Java转换器

  3. 不要在任何给定时间加载超过您需要的FXML,例如如果你现在不打算显示它,请不要加载它。

  4. 在后台线程中加载FXML(对某些控件类型不起作用,例如Tooltip,在Java 8)。

  5. 加载初始登录或启动画面,然后在显示启动或登录屏幕时加载其余的FXML。在登录屏幕上接受凭据后,我在某些应用程序中加载了FXML。

  6. 通过删除不必要的节点来降低应用程序UI的复杂性。

  7. 确保您没有在控制器的初始化方法中对JavaFX UI线程执行不必要的工作(例如,读取数据库)。

  8. FXMLLoader.load() JavaFX 8中的文档包含对FXML模板的引用,但我不认为它曾经实现过,所以它可能对你没有帮助。

  9. 我听说提到引用来自FXML的静态方法很慢。

  10. 使用 JavaFX的最新开发版本

  11. 一旦你从FXML加载了一个节点层次结构,稍后重用该层次结构而不是将其丢弃并加载新的层次结构(例如,如果你有一个使用FXML构建内容的对话窗口,当用户关闭对话框时,只需隐藏它,当你需要再次显示一个类似的对话框时,只需显示隐藏的对话框而不是构建一个全新的对话框和场景图)。

  1. One should NOT use the static FXMLLoader.load() but should instead create an instance of FXMLLoader and reuse that one using the instance loader.load() method. The reason is that the FXML-Loader caches, e.g. class lookups, and hence is faster on loading the next FXML-File.
  2. Use a tool to compile the FXML to Java, such as Tom Schindl's FXML compiler or the NetBeans FXML to Java converter.
  3. Don't load any more FXML than you need to at any given time, e.g. if you aren't going to be displaying it right now, don't load it.
  4. Load the FXML in a background thread (doesn't work for some control types, e.g. Tooltip, in Java 8).
  5. Load an initial login or splash screen first and then load the rest of the FXML while the splash or login screen is displayed. I load FXML in some apps after the credentials are accepted at the login screen.
  6. Reduce the complexity of your application UI by removing unnecessary nodes.
  7. Ensure you aren't doing unnecessary work (e.g. reading a database) on the JavaFX UI thread in the initialize methods of your controllers.
  8. The FXMLLoader.load() documentation in JavaFX 8 includes a reference to FXML templates, but I don't think that was ever implemented, so maybe it won't help you.
  9. I heard mention that referring to static methods from FXML is slow.
  10. Use the latest development version of JavaFX.
  11. Once you have loaded a node hierarchy from FXML, reuse the hierarchy later rather than throwing it away and loading a new one (e.g. if you have a dialog window with contents constructed using FXML, when the user closes the dialog, just hide it and when you need to show a similar dialog again, just show the hidden dialog rather than constructing a whole new dialog and scene graph).

反思是FXML缓慢的原因

我认为Java 8 FXML实现缓慢的关键原因在于它非常依赖于反射和反射很慢反思教程中所述:

I think the key reason the Java 8 FXML implementation is slow is that it relies so heavily on reflection and reflection is slow as described in the reflection tutorial:


由于反射涉及动态解析的类型,因此无法执行某些Java虚拟机优化。因此,反射操作的性能低于非反射操作,并且应避免在对性能敏感的应用程序中频繁调用的代码段中。

Because reflection involves types that are dynamically resolved, certain Java virtual machine optimizations can not be performed. Consequently, reflective operations have slower performance than their non-reflective counterparts, and should be avoided in sections of code which are called frequently in performance-sensitive applications.

这篇关于有没有办法快速编译/加载fxml文件只有一次,而不是每次重启应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆