MinhHoang
MinhHoang

Reputation: 711

Worker and daemon thread in java

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

Answers (2)

Jeffrey
Jeffrey

Reputation: 44808

There are two types of Threads: daemons and non-daemons. The JVM will cease execution when there all non-daemon Threads stop running.

SwingWorkers 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

michael nesterenko
michael nesterenko

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

Related Questions