Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why to use SwingUtilities.invokeLater in main method?

Tags:

After years of Java programming I always used to create my main() methods like this :

public static void main(String[] args)  {     runProgram(); } 

But recently I studied some codes from the Web and saw this sometimes instead of the usual main() use above :

public static void main(String[] args)  {     SwingUtilities.invokeLater(new Runnable()      {         public void run()          {             runProgram();         }     }); } 

I simply want to know :

  • Why to use this instead of the usual main() way ? I can't see any difference when I give it a try.
  • What is the difference between these two ways ?

Thanks for reading me and your answers.

like image 530
Rob Avatar asked Mar 08 '13 19:03

Rob


People also ask

What is SwingUtilities invokeLater () used for?

An invokeLater() method is a static method of the SwingUtilities class and it can be used to perform a task asynchronously in the AWT Event dispatcher thread. The SwingUtilities. invokeLater() method works like SwingUtilities. invokeAndWait() except that it puts the request on the event queue and returns immediately.

What is the difference between InvokeAndWait and invokeLater?

Difference on InvokeLater vs InvokeAndWait in Swing 1) InvokeLater is used to perform a task asynchronously in AWT Event dispatcher thread while InvokeAndWait is used to perform task synchronously. 2) InvokeLater is a non-blocking call while InvokeAndWait will block until the task is completed.

What is SwingUtilities invokeLater new runnable ()?

SwingUtilities class has two useful function to help with GUI rendering task: 1) invokeLater(Runnable):Causes doRun. run() to be executed asynchronously on the AWT event dispatching thread(EDT). This will happen after all pending AWT events have been processed, as is described above.

What is Java SwingUtilities?

As stated in the API, SwingUtilities is a collection of utility methods for Swing. In this case, it is needed to ensure that Swing components are created/modified in the Event Dispatch Thread, or EDT . Also, as stated in the API, invokeLater is used when an application thread needs to update the GUI.


2 Answers

The docs explain why. From Initial Threads

Why does not the initial thread simply create the GUI itself? Because almost all code that creates or interacts with Swing components must run on the event dispatch thread.

and from The Event Dispatch Thread

Some Swing component methods are labelled "thread safe" in the API specification; these can be safely invoked from any thread. All other Swing component methods must be invoked from the event dispatch thread. Programs that ignore this rule may function correctly most of the time, but are subject to unpredictable errors that are difficult to reproduce.

like image 129
Reimeus Avatar answered Sep 30 '22 15:09

Reimeus


Because the thread "main" started by VM is not the event dispatch thread.

like image 23
sol4me Avatar answered Sep 30 '22 15:09

sol4me