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

查看:47
本文介绍了在应用程序线程外更新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 Application线程)进行访问和修改.

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应用程序线程上; currentThread =任务

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 Application Thread上.

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天全站免登陆