On which thread JavaFX change listeners are executed ?

家住魔仙堡 提交于 2019-12-10 23:35:08

问题


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
                    }

        });

Or do I need to add Platform.runLater() ?

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.


回答1:


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.

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.




回答2:


Yes, the code runs on the JFX-Thread.

JavaFx Architecture Documentation Check out the Section "Glass Windowing Toolkit -> Threads"



来源:https://stackoverflow.com/questions/38345701/on-which-thread-javafx-change-listeners-are-executed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!