Reputation: 711
I have read tutorial concerning background thread(or worker thread) and now I get confused between SwingWorker and daemon thread, are they the same? If i'm writing non GUI application should i create SwingWorker or daemon thread since they both do the job in background?
Upvotes: 0
Views: 1982
Reputation: 44808
There are two types of Thread
s: daemons and non-daemons. The JVM will cease execution when there all non-daemon Thread
s stop running.
SwingWorker
s are a utility for performing some time expensive task asynchronously from the Event Dispatch Thread to prevent your GUI from becoming unresponsive. A SwingWorker
is not a Thread
, it is a Runnable
task that can be sumbitted to a Thread
for execution.
If you are not doing anything with a GUI, use a Thread
. If you need to perform an action on a separate Thread
for your GUI, use a SwingWorker
.
Upvotes: 1
Reputation: 14439
SwingWorker is a specialized class that eases interacting with user interface. Access to gui components should be done only from special gui thread. SwingWorker has method done
which is guaranteed to be executed in gui thread, so you can safely update ui.
If you do not interact with swing ui you should use simple Thread. Moreover if you need a few threads its good to look at ExecutorService
which is a thread pool.
Upvotes: 0