JavaFX 9: Expose PlatformImpl.runAndWait() as Platform.runAndWait()

Stefan Fuchs snfuchs at gmx.de
Wed Nov 2 20:02:17 UTC 2016


Hi,

in my experience blocking your working thread for the JavaFX application 
thread is almost always a sign of bad application design.
You can easily deadlock your application. In our quite complex JavaFX 
Application, we could eliminate almost all of its uses.

However, in one case it is still needed: In a thread we sequentially 
prepare several images in the background, create a snapshot and save 
them as jpegs.
Here we use java.util.concurrent.CountDownLatch to simulate 
PlatformImpl.runAndWait.

         final WritableImage image = new WritableImage((int) 
targetWidth, (int) targetHeight);
         final CountDownLatch countDownLatch = new CountDownLatch(1);
         Platform.runLater(() -> {

                  /* render  and snapshot image */

                  countDownLatch.countDown();
          });

         boolean released = countDownLatch.await(10, TimeUnit.SECONDS);
         if (!released) {
             Logger.getLogger(getClass()).error("Timeout reached, while 
waiting for snapshot");
             return null;
         }

         return image;



What I would like to see implemented is a method, we call runNowOrLater.

That is, if I'm on the JavaFX ApplicationThread execute the Runnable 
immediately, if not call Platform.runLater().
With this method I have not to worry, if I'm on the JavaFX Application 
thread or not and I avoid flooding the event queue with calls to 
Platform.runLater, that could have been executed directly.

So basically:

     public static void runNowOrLater(final Runnable runnable) {
         if (Platform.isFxApplicationThread()) {
             runnable.run();
         } else {
             Platform.runLater(runnable);
         }
     }

-- Stefan



> Hi, Kevin. Thanks for the info!
>
> On Wed, Nov 2, 2016 at 5:36 PM, Kevin Rushforth <kevin.rushforth at oracle.com>
> wrote:
>
>> No. This isn't something we will do for JDK 9, which is feature complete
>> (with an exception process for critical requests, especially relating to
>> Jigsaw). I note in this case that it isn't clear whether we want to do this
>> one at all.
>>
>> -- Kevin
>>
>>
>>
>> Benjamin Gudehus wrote:
>>
>>> Hey!
>>>
>>> Are there plans to expose this API [1] in JavaFX 9?
>>>
>>> // NOTE: Add the following if we decide to expose it publicly
>>> // public static void runAndWait(Runnable runnable) {
>>> // PlatformImpl.runAndWait(runnable);
>>> // }
>>>
>>> [1] http://hg.openjdk.java.net/openjfx/9-dev/rt/file/
>>> 6edd0c3c01f4/modules/javafx.graphics/src/main/java/javafx/
>>> application/Platform.java#l148
>>>
>>> --Benjamin
>>>
>>>



More information about the openjfx-dev mailing list