Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between SwingWorker and SwingUtilities.invokeLater

I need to run some method in Swing application in separate thread. What is the difference between using SwingWorker and SwingUtilities.invokeLater. Which one should I use to run a thread in Swing application? I couldn't find exact info in the tutorial at

http://java.sun.com/docs/books/tutorial/uiswing/concurrency/index.html

like image 262
Pawel Szulc Avatar asked Mar 18 '10 17:03

Pawel Szulc


2 Answers

SwingUtilities.invokeLater is used if you have something that must run in the EDT.

If you have a long-running task, you instead need to use a SwingWorker, since it does not run on the EDT and therefore does not cause the GUI to freeze while it runs.

like image 115
Michael Myers Avatar answered Sep 21 '22 22:09

Michael Myers


It looks like you would want to:

  • use SwingWorker when you need to monitor the status of a long-running background process

  • use SwingUtilities.invokeLater if you just want a short task to run but do not need feedback on it. Sort of like a fire-and-forget. Just keep in mind it will run in the AWT event dispatching thread, so your application would not be getting any events handled while the task is running.

like image 45
Justin Ethier Avatar answered Sep 17 '22 22:09

Justin Ethier