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

Animation and AnimationTimer methods must be called on JavaFX Application thread

XMLWordPrintable

    • Icon: CSR CSR
    • Resolution: Approved
    • Icon: P4 P4
    • jfx22
    • javafx
    • None
    • behavioral
    • minimal
    • Hide
      The new threading restriction enforcement is backward compatible. Applications that attempt to call the Animation or AnimationTimer methods on the wrong thread today will already fail in unpredictable ways; with this change they will now get a consistent exception with enough information to fix their application.
      Show
      The new threading restriction enforcement is backward compatible. Applications that attempt to call the Animation or AnimationTimer methods on the wrong thread today will already fail in unpredictable ways; with this change they will now get a consistent exception with enough information to fix their application.
    • Java API
    • JDK

      Summary

      Add a check to the Animation and AnimationTimer public methods to verify that these are called from the JavaFX Application thread, and update JavaDoc accordingly.

      Problem

      Currently the Animation (play(), pause(), and stop()) and AnimationTimer (play(), stop()) public methods don't perform any check to verify if they are called from the JavaFX Application thread.

      However, all animations run on the FX Application Thread, and if the call is done from a different thread, unexpected errors appear (typically NPEs like the ones in JDK-8159048) without a clear explanation that allows the developer preventing those.

      Solution

      Add a check to the Animation and AnimationTimer public methods to verify that these are called from the JavaFX Application thread, and if the call is done from any other thread, throw an IllegalStateException with "Not on FX application thread" message.

      Update the javadoc of the Animation and AnimationTimer classes and public methods to indicate that these run on the JavaFX Application Thread, and that these will throw and IllegalStateException if called from any other thread.

      Specification

      The following is added to the Animation and AnimationTimer javadoc:

      /* ...
       * The animation runs on the JavaFX Application Thread.
       * ...
       */
      public abstract class Animation {...}
      
      /* ...
       * The animation timer runs on the JavaFX Application Thread.
       * ...
       */
      public abstract class AnimationTimer {...}

      The following is added to the public methods of Animation (play(), pause(), and stop()) and AnimationTimer (play(), stop()):

       /* ...
       * This method must be called on the JavaFX Application thread.
       *
       * @throws IllegalStateException if this method is called on a thread
       *                other than the JavaFX Application Thread,...
       */
      public void play() {
          Toolkit.getToolkit().checkFxUserThread();
           ...
      }
      
       /* ...
       * This method must be called on the JavaFX Application thread.
       *
       * @throws IllegalStateException if this method is called on a thread
       *                other than the JavaFX Application Thread,...
       */
      public void pause() {
          Toolkit.getToolkit().checkFxUserThread();
           ...
      }
      
       /* ...
       * This method must be called on the JavaFX Application thread.
       *
       * @throws IllegalStateException if this method is called on a thread
       *                other than the JavaFX Application Thread,...
       */
      public void stop() {
          Toolkit.getToolkit().checkFxUserThread();
           ...
      }

            jpereda Jose Pereda
            webbuggrp Webbug Group
            Kevin Rushforth
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: