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

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


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 

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

                  /* render  and snapshot image */


         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()) {
         } else {

-- Stefan

> Hi, Kevin. Thanks for the info!
> On Wed, Nov 2, 2016 at 5:36 PM, Kevin Rushforth <kevin.rushforth at>
> 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]
>>> 6edd0c3c01f4/modules/
>>> application/
>>> --Benjamin

More information about the openjfx-dev mailing list