问题
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