invokeAndWait

Kevin Rushforth kevin.rushforth at oracle.com
Fri Jan 4 07:53:03 PST 2013


Btw, if you want to implement your own, you can take a look at the 
runAndWait() method in PlatformImpl.java if you like. We use it 
internally but for the reasons Richard gave, decided not to make it 
public API.

Here is a method based on the one in PlatformImpl if anyone is interested.

-- Kevin


-----------------------------------------------------------------------------------------------------
    public static void runAndWait(final Runnable r) {
        if (Platform.isFxApplicationThread()) {
             try {
                 r.run();
             } catch (Throwable t) {
                 System.err.println("Exception in runnable");
                 t.printStackTrace();
             }
        } else {
            final CountDownLatch doneLatch = new CountDownLatch(1);
            Platform.runLater(new Runnable() {
                @Override public void run() {
                    try {
                        r.run();
                    } catch (Throwable t) {
                        System.err.println("Exception in runnable");
                        t.printStackTrace();
                    } finally {
                        doneLatch.countDown();
                    }
                }
            });

            try {
                doneLatch.await();
            } catch (InterruptedException ex) {
                ex.printStackTrace();
            }
        }
    }
-----------------------------------------------------------------------------------------------------


Mario Torre wrote:
> Yeah, I fully agree with the rationale given by Richard, invokeAndWait
> has been (and still is) one of the main sources of troubles for many
> poorly written Java programs, is really too easy to get this done
> wrong, so omitting it from the core API was a wise decision imho,
> people who know what they are doing can leverage the same
> functionality with little effort.
>
> Cheers,
> Mario
>
> 2013/1/2 Hendrik Ebbers <hendrik.ebbers at me.com>:
>> Thanks.
>>
>> Am 02. Januar 2013 um 05:12 schrieb Gerrit Grunwald 
>> <han.solo at muenster.de>:
>>
>>
>> Hi Hendrik,
>>
>> there is a comment from Richard about it that could be found here:
>>
>> https://forums.oracle.com/forums/thread.jspa?threadID=2370263
>>
>> Cheers,
>>
>> Gerrit
>>
>> Am 01.01.2013 um 23:48 schrieb Hendrik Ebbers <hendrik.ebbers at me.com>:
>>
>> Hi,
>>
>> I created a "invokeAndWait" method for JavaFX that is a equivalent of
>> SwingUtilities.invokeAndWait(...). It will be part of DataFX. You can 
>> read
>> about it in my blog:
>> http://www.guigarage.com/2013/01/invokeandwait-for-javafx/
>>
>> I ask myself why there is no "invokeAndWait" method in
>> javafx.application.Platform. Is there any good reason? In swing I use
>> "invokeAndWait" often and so I think it is also important for JavaFX.
>>
>> - Hendrik
>
>
>


More information about the openjfx-dev mailing list