Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8201635

com.sun.javafx.binding.ExpressionHelper#addListener() not thread safe

XMLWordPrintable

    • x86_64
    • windows_10

      A DESCRIPTION OF THE PROBLEM :
      I ran into the same issue as reported in JDK-8164446. The issue has been closed because it was missing a reproducer. I was able to create a reproducer here: https://gist.github.com/micheljung/b171b4e397306b5541ebd2fab7422538

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      See https://gist.github.com/micheljung/b171b4e397306b5541ebd2fab7422538

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      No exception
      ACTUAL -
      java.lang.ArrayIndexOutOfBoundsException: 5
      at javafx.base/com.sun.javafx.binding.ExpressionHelper$Generic.addListener(ExpressionHelper.java:294)
      at javafx.base/com.sun.javafx.binding.ExpressionHelper.addListener(ExpressionHelper.java:68)
      at javafx.base/javafx.beans.property.StringPropertyBase.addListener(StringPropertyBase.java:86)
      at JDK8164446.lambda$start$1(JDK8164446.java:14)
      at java.base/java.lang.Thread.run(Thread.java:844)

      ---------- BEGIN SOURCE ----------
      import javafx.application.Application;
      import javafx.beans.property.SimpleStringProperty;
      import javafx.beans.property.StringProperty;
      import javafx.stage.Stage;

      public class JDK8164446 extends Application {

        @Override
        public void start(Stage primaryStage) {
          StringProperty stringProperty = new SimpleStringProperty();

          new Thread(() -> {
            while (true) {
              stringProperty.addListener((observable, oldValue, newValue) -> noop());
            }
          }).start();

          while (true) {
            stringProperty.addListener((observable, oldValue, newValue) -> noop());
          }
        }

        private static void noop() {

        }

        public static void main(String[] args) {
          launch(args);
        }
      }

      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Only add listeners in the JavaFX Application thread or synchronize on every property where a listener is added.

      FREQUENCY : always


            fmatte Fairoz Matte
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: