javaswingevent-dispatch-threadeventqueueinvokeandwait

Java API "Run on EDT if not on EDT"


Just a musing about some repetitive code I have:

Runnable run = new Runnable() {
  @Override
  public void run() {
    // Some EDT code
  }
};

if (!EventQueue.isDispatchThread()) {
  SwingUtilities.invokeAndWait(run);
} else {
  run.run();
}

It's not terribly annoying, but it seems like there would be some proprietary function that checks this for you, although I haven't found it.


Solution

  • it seems like there would be some proprietary function that checks this for you

    There isn't.