Java Swing UI Thread Event runLaterAndWait(final Runnable runnable)

Here you can find the source of runLaterAndWait(final Runnable runnable)

Description

Run the given runnable in the EDT and wait until its finished.

License

Open Source License

Declaration

public static void runLaterAndWait(final Runnable runnable) 

Method Source Code

//package com.java2s;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

import javax.swing.SwingUtilities;

public class Main {
    private static final int RUN_LATER_AND_WAIT_TIMEOUT = 500;

    /**/*from  w ww  . ja  va2 s  .  co  m*/
     * Run the given runnable in the EDT and wait until its finished.
     */
    public static void runLaterAndWait(final Runnable runnable) {
        final CountDownLatch l_latch = new CountDownLatch(1);
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                runnable.run();
                l_latch.countDown();
            }
        });

        while (true) {
            try {
                if (l_latch.await(RUN_LATER_AND_WAIT_TIMEOUT, TimeUnit.MILLISECONDS)) {
                    break;
                }
            } catch (InterruptedException e) {
            }
        }
    }
}

Related

  1. runInEventDispatchThread(Runnable runnable)
  2. runInEventDispatchThreadAndWait(final Runnable r)
  3. runInSwingThread(final Runnable runnable)
  4. runInSwingThread(Runnable runnable)
  5. runLater(Runnable runnable)
  6. runModalProcess(String title, final Runnable runnable)
  7. runNowInEdt(Runnable runnable)
  8. runOnDispatchThread(Runnable runnable)
  9. runOnEDT(final Runnable r)