views:

226

answers:

2

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

+5  A: 

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.

Michael Myers
thank you. this is generaly what i thought, but i needed confiramtion
Paul Szulc
+3  A: 

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.

Justin Ethier