Poornima Prakash
Poornima Prakash

Reputation: 330

what does this method invokeOnEventDispatchThread do?

I came across the following piece of code in my code base and I dont understand how it works.I'm a bit of a novice at Swing, hence sorry if it seems like a silly question.

public static void invokeOnEventDispatchThread(Runnable r){
try{
  if(SwingUtilities.isEventDispatchThread()){
    r.run();
  }else{
    SwingUtilities.invokeAndWait(r);
  }
}catch(Exception e){
  ;
}

}

Here will r.run() be invoked immediately in the event dispatch thread? Is the point of the method that r.run() be called asap, moving it to the head of the queue?

Thanks.

Upvotes: 1

Views: 23

Answers (1)

assylias
assylias

Reputation: 328598

The point of the method is to run the code synchronously:

  • either you already are executing in the EDT and the code is simply run
  • or you aren't and the method will wait until the runnable has been executed by the EDT

In particular, the javadoc for invokeAndWait states that the method should not be called on the EDT hence the 2 branches in your code.

Upvotes: 1

Related Questions