需要在JavaFX Application Thread中运行的方法列表? [英] List of Methods required to be run in JavaFX Application Thread?

查看:265
本文介绍了需要在JavaFX Application Thread中运行的方法列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试开发JavaFX应用程序(游戏)。为了保持应用程序顺利运行,我尝试将尽可能多的程序逻辑放到一个单独的线程中。但是,我经常遇到一些JAVAFX API调用需要在JFX Application线程中运行的问题。例如。创建一组矩形并对其进行着色可以在一个线程中完成 - 将它们添加到Group时,调用.doLayout()和.applycss()需要JFX Appl。线。
我也意识到.play()转换也需要在JFX应用程序线程中运行。

I'm currently trying to develop an JavaFX Application (Game). In order to keep the application run smoothly, I try to lay off as much of the program logic into a separate thread. However, I often stumble into the problem that some JAVAFX API Calls require to be run in JFX Application thread. E.g. creating a set of rectangles and coloring it, can be done in a thread - while adding them to a "Group", calling .doLayout() and .applycss() requires JFX Appl. thread. I also realized that ".play()" a transition is also required to be run in JFX Application thread.

问题是 - 如果你不是在正确的线程中执行它 - 数组越界异常可能在JFX中随机发生,这并不指向根本原因。总是很难确定程序和图形逻辑是可以卸载到线程还是需要在JFX应用程序线程中运行。

The problem is - if you are not executing it in the correct thread - an "array out of bounds" exception may occur randomly within JFX which does not point you to the root cause. It always hard to determine whether the program and graphic logic can be offloaded to a thread or is required to be run in JFX Application Thread.

是否有人知道JavaFX列表需要在JFX应用程序线程下运行的方法调用?

Does anybody know a list of JavaFX method calls which require to be run under JFX Application Thread?

全部谢谢!

推荐答案

根据 应用程序类文档(这也在JavaFX文档的其他地方指定):

According to the Application class documentation (and this is also specified in other places in the JavaFX documentation):


必须在JavaFX应用程序线程上创建JavaFX场景和舞台对象以及将场景图形操作修改为活动对象(已经附加到场景的那些对象)。

Creation of JavaFX Scene and Stage objects as well as modification of scene graph operations to live objects (those objects already attached to a scene) must be done on the JavaFX application thread.

因此,您可以在后台线程上创建新节点(例如矩形),并且可以修改它们的属性(例如color m)在后台线程上,只要它们没有附加到场景。将节点附加到场景后,必须在FX应用程序线程上对节点进行任何修改。

So you can create new nodes (e.g. rectangles) on a background thread, and you can modify their properties (e.g. "color them") on a background thread as long as they have not been attached to a scene. Once the node is attached to a scene, any modification of the node must occur on the FX Application Thread.

如果窗格或组附加到场景,则调用 getChildren()。add(...)修改该窗格/组,因此必须在FX应用程序线程上执行。您引用的其他示例( layout() applyCSS())仅在相关节点附加到无论如何,一个场景。

If a pane or group is attached to a scene, then calling getChildren().add(...) modifies that pane/group, so it must be performed on the FX Application Thread. The other examples you cite (layout() and applyCSS()) only make sense if the relevant node is attached to a scene anyway.

这篇关于需要在JavaFX Application Thread中运行的方法列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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