有没有一种方法可以更快地编译/加载 fxml 文件并且只加载一次,而不是在每次重新启动应用程序时? [英] Is there a way to compile / load fxml files faster and only one time, and not at every restart of an application?

查看:15
本文介绍了有没有一种方法可以更快地编译/加载 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();
    }

    ....

}

所有这些信息控制器也有其他 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(不适用于某些控件类型,例如 Java 8 中的工具提示).
  5. 加载初始登录或启动画面 首先,然后在显示初始屏幕或登录屏幕时加载 FXML 的其余部分.在登录屏幕接受凭据后,我在某些应用中加载 FXML.
  6. 通过删除不必要的节点来降低应用 UI 的复杂性.
  7. 确保您没有在控制器的初始化方法中的 JavaFX UI 线程上执行不必要的工作(例如读取数据库).
  8. FXMLLoader.JavaFX 8 中的 load() 文档包括对 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天全站免登陆