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 开发者_JAVA技巧exact info in the tutorial at
http://java.sun.com/docs/books/tutorial/uiswing/concurrency/index.html
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.
It looks like you would want to:
use
SwingWorker
when you need to monitor the status of a long-running background processuse
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.
精彩评论