elecengin
elecengin

Reputation: 143

Lazy Initialization of Properties in JavaFX

It seems that lazy initialization of properties is a frequent design pattern in JavaFX. For example, the OpenJFX Labeled implementation contains the following snippet:

public final StringProperty textProperty() {
    if (text == null) {
        text = new SimpleStringProperty(this, "text", "");
     }
     return text;
}

Given that the property is conceivably accessed from the JavaFX UI thread as well as other potential client threads, and this implementation is not thread safe, this seems like a bug. Given how frequently the pattern is used this is either a very serious issue or accessing the property on the control is not intended to be thread safe (and I am misunderstanding the JavaFX concurrency strategy).

Can anyone explain the rationale for not making the lazy initialization thread-safe?

Upvotes: 1

Views: 786

Answers (1)

Andy Till
Andy Till

Reputation: 3511

JavaFX is not thread safe, it is thread confined just like Swing. In fact, many methods in the JavaFX API will throw an exception if you do not use it from the JavaFX thread.

Upvotes: 5

Related Questions