在应用程序线程之外更新 JavaFX Live 节点 [英] Update JavaFX Live nodes outside Application Thread

查看:17
本文介绍了在应用程序线程之外更新 JavaFX Live 节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

JavaFX 应用程序线程.我能找到的消息来源说节点上的所有更新都必须在此线程中进行.

The JavaFX Application Thread. Sources I can find says all updates on nodes must happen in this thread.

我正在尝试查找有关此规则的文档以及此规则是否有任何例外.https://docs.oracle.com/javase/8/javafx/interoperability-tutorial/concurrency.htm正如它所说:

I am trying to find the documentation for this and if there are any exceptions to this rule. https://docs.oracle.com/javase/8/javafx/interoperability-tutorial/concurrency.htm As it says:

代表 JavaFX 应用程序图形用户界面的 JavaFX 场景图不是线程安全的,只能从 UI 线程(也称为 JavaFX 应用程序线程)访问和修改.

The JavaFX scene graph, which represents the graphical user interface of a JavaFX application, is not thread-safe and can only be accessed and modified from the UI thread also known as the JavaFX Application thread.

https://docs.oracle.com/javase/8/javafx/get-started-tutorial/jfx-architecture.htm#A1107438

任何实时"场景,即属于窗口的场景,都必须从此线程访问.可以在后台线程中创建和操作场景图,但是当其根节点附加到场景中的任何活动对象时,必须从 JavaFX 应用程序线程访问该场景图.

Any "live" scene, which is a scene that is part of a window, must be accessed from this thread. A scene graph can be created and manipulated in a background thread, but when its root node is attached to any live object in the scene, that scene graph must be accessed from the JavaFX application thread.

我曾经历过,并非节点上的所有更新都必须在 JavaFX AT 上完成.一些更新节点的调用在该线程之外工作正常.例如,更新 Text textProperty 不需要在 JavaFX AT 中运行.因此,设置工具提示或更改可见性/禁用/管理似乎也不行.

I have experienced that not all updates on a node must be done on the JavaFX AT. Some calls to update the node works fine outside this thread. For instance updating Text textProperty does not require running within the JavaFX AT. Nor thus it looks like setting tooltip does either, or changing visibility/disable/managed.

JavaFX AT 之外的 Label textProperty 更新将抛出一个

Updates on Label textProperty outside of the JavaFX AT will throw an

IllegalStateException:不在 FX 应用程序线程上;当前线程 = 任务

IllegalStateException: Not on FX application thread; currentThread = Task

推荐答案

您将不允许的内容与引发异常的内容混淆了.仅仅因为某些东西没有抛出异常并不意味着它是被允许的,或者它是安全的,或者它可以保证工作.

You are confusing what is not allowed with what throws exceptions. Just because something doesn't throw an exception doesn't mean it is allowed, or that it is safe, or that it is guaranteed to work.

对属于实时场景图一部分的节点的所有更改必须发生在 JavaFX 应用程序线程上.

All changes to nodes that are part of a live scene graph must happen on the JavaFX Application Thread.

如果违反此规则,JavaFX 会尽最大努力抛出异常.检查线程需要时间,对于一些操作来说,检查线程的性能成本太高,所以不是所有的违反规则都会导致异常.然而,违反规则很容易在未来的任意时间出现不一致的行为,即使没有抛出异常.请参阅移动圆圈随机消失 (javafx),了解实际发生的这种情况的示例.

JavaFX makes a best effort to throw exceptions if this rule is violated. Checking the thread costs time, and for some operations the performance cost of checking the thread is too high, so not all violations of the rule will result in an exception. However, violating the rule is prone to inconsistent behavior at arbitrary times in the future, even if no exception is thrown. See Moving circle randomly disappears (javafx) for an example of this happening in practice.

这篇关于在应用程序线程之外更新 JavaFX Live 节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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