1

I am writing my code in swing. To launch a private function of another class that can be called in awt thread . i simply write the code of that function in my class in SwingUtilities.invokeLater thread.

main() {

    SwingUtilities.invokeLater(new Runnable() {

        //code of private function

    });

}

but after executing this control is not coming back to main thread. Can anyone suggest what is happening and how to resolve this?

aioobe
  • 413,195
  • 112
  • 811
  • 826
Karn
  • 79
  • 8

2 Answers2

3

SwingUtilities.invokeLater will not block the execution of the current thread. If believe it does, you can simply do

System.out.println("Before");

SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            //code of private function
        }
    });

System.out.println("After");

and you will see Before and After printed right after each other more or less immediately.

(Make sure you haven't accidentally used SwingUtilities.invokeAndWait.)

aioobe
  • 413,195
  • 112
  • 811
  • 826
1

SwingUtilities.invokeLater(Runnable) will execute the code in the EDT as you have correctly stated. main thread continues its execution right after the call of invokeLater. You may not see the main thread if you execute the program in debugger and having a breakpoint somewhere in the code executed in EDT, but the main thread is executed for sure.

Boris Pavlović
  • 63,078
  • 28
  • 122
  • 148