Thread vs. Daemon Thread.

What are the differences between a normal thread and a daemon thread?
[76 byte] By [BinaryDecimala] at [2007-11-27 1:39:07]
# 1
It's explained here http://java.sun.com/docs/books/tutorial/essential/concurrency/and here http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Thread.html
jverda at 2007-7-12 0:51:46 > top of Java-index,Java Essentials,Java Programming...
# 2

OK, I went through the links but couldn't find why would you set a thread as daemon. I noticed in some codes that if a thread is daemon then use SwingUtilities.invokeLater( theThread ) when it is used for GUI. In the code:

if( Thread.currentThread().isDaemon() ){

Runnable theThread = new Runnable(){

public synchronized void run(){

doSomething();

}

};

SwingUtilities.invokeLater( theThread );

}else{

doSomething();

}

This code confuses me why when it is a daemon you invoke later and when it is not you just right a way? Plus why would set a thread as a daemon satrting with?

Thanks,

BinaryDecimala at 2007-7-12 0:51:46 > top of Java-index,Java Essentials,Java Programming...
# 3

You set a thread as a deamon if you don't want that thread's existence to keep the VM running.

So you might have one thread that's a master conroller for your app, and then you might have a bunch of worker threads that put requests into a queue and others that service that queue. If the controller dies, and you want the app to stop processing because of that, then having the controller be a non-daemon an the workers be daemons will accomplish that.

I don't to GUIs, so I don't know how it fits in there, but one plausible scenario might be that there's a thread that's just responsible for drawing the GUI. If your main processing thread receives a quit command, it can exit, and there's no reason for the GUI thread to keep drawing, so if it's a deamon, the VM will notice that all that's left is that daemon thread, and then exit.

jverda at 2007-7-12 0:51:46 > top of Java-index,Java Essentials,Java Programming...
# 4
When you post code, please use[code] and [/code] tags as described in [url= http://forum.java.sun.com/help.jspa?sec=formatting]Formatting tips[/url] on the message entry page. It makes it much easier to read.
jverda at 2007-7-12 0:51:46 > top of Java-index,Java Essentials,Java Programming...
# 5
Thanks for your reply. It is very useful. And thanks for the tip. Next time I will use tags . :)
BinaryDecimala at 2007-7-12 0:51:46 > top of Java-index,Java Essentials,Java Programming...
# 6

I just want to clarify something. Based on your first sentance I understood that if the main thread dies, the daemon ones will stay alive until they are finihsed. If this is true, does this mean that the daemon thread is visible and handled by the hosting OS and when it is not daemon it means that it is invisible to the OS and handled by the JVM?

BinaryDecimala at 2007-7-12 0:51:46 > top of Java-index,Java Essentials,Java Programming...
# 7

> I just want to clarify something. Based on your first

> sentance I understood that if the main thread dies,

> the daemon ones will stay alive until they are

> finihsed.

No, exactly the opposite.

The VM exits as soon as the last non-deamon thread ends.

jverda at 2007-7-12 0:51:47 > top of Java-index,Java Essentials,Java Programming...
# 8
Thanks,
BinaryDecimala at 2007-7-12 0:51:47 > top of Java-index,Java Essentials,Java Programming...