1

As I know all changes to javafx scene must be made on javafx thread. However, I run the following code and I don't get any exceptions.

  TextArea mainTextArea = ...
  new Thread(()->{
        for (int i = 0; i < 10; i++){
          mainTextArea.appendText(String.valueOf(i));
          mainTextArea.positionCaret(mainTextArea.getText().length());
        }
    }).start();

It seems that I miss something. Please, explain.

Pavel_K
  • 10,748
  • 13
  • 73
  • 186
  • You might also be interested in this related question for [multi-threaded logging with JavaFX](https://stackoverflow.com/questions/24116858/most-efficient-way-to-log-messages-to-javafx-textarea-via-threads-with-simple-cu). – jewelsea May 22 '17 at 17:41

1 Answers1

4

Just because you don't get any exceptions, doesn't mean it is safe. There is no guarantee this will work and it is prone to failure (i.e. arbitrarily seeing corrupt data) at any time.

The JavaFX API makes a best effort to throw exceptions when the threading rules are violated, but there are some circumstances where this is not done (basically, the cost of checking on which thread the method is executed is too expensive in some operations). Historically, prior to JavaFX 2.2, no exceptions were thrown at all (similar to Swing); however the requirement that UI components that are part of a scene graph must only be accessed on the FX Application Thread still exists.

James_D
  • 201,275
  • 16
  • 291
  • 322