在哪个线程上执行JavaFX更改侦听器? [英] On which thread JavaFX change listeners are executed ?

查看:213
本文介绍了在哪个线程上执行JavaFX更改侦听器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的文档更改侦听器中添加的代码是否总是在JavaFX应用程序线程中执行?

Is the code added in following document change listener will always get executed in JavaFX application thread ?

webEngineObject.documentProperty().addListener(new ChangeListener<Document>(){

                    @Override
                    public void changed(
                            ObservableValue<? extends Document> arg0,
                            Document arg1, Document arg2) {                     
                        //some code here
                    }

        });

或者我是否需要添加 Platform.runLater()

当我在那里遇到断点后查看线程堆栈时,看起来代码在JavaFX应用程序线程本身中执行,但是想确认无法找到任何评论关于此的文件。提及此文档的任何链接都会非常有用。

When I looked at thread stack after hitting a breakpoint there, it looks like code was getting executed in JavaFX Application Thread itself, but wanted to confirm as couldn't find any comment in documentation regarding this. Any link to documentation which mentioned this would be really helpful.

推荐答案

一般来说,更改侦听器在同一个线程上运行,更改是制作。当然,可能会有 Property ObservableValue 的实现,它们在另一个线程上调用侦听器,但就我而言我知道没有这种行为的默认实现。

Generally speaking, change listeners run on the same thread the change was made on. Of course, there could be implementations of Property or ObservableValue which call the listener on another thread, but as far as I'm aware there are no default implementations with this behavior.

所以简单的答案是 - 更改侦听器将在原始更改执行的任何线程上运行。

如果说属性是与JavaFX节点相关的原始更改应该在JavaFX线程上调用,以防止Not on FX应用程序线程异常。

So the simple answer is - the change listener will run on whichever thread the original change was performed.
That being said, if the property is related to a JavaFX node, the original change should have been called on the JavaFX thread to prevent a "Not on FX application thread" exception.

这篇关于在哪个线程上执行JavaFX更改侦听器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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