This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Threads and Synchronization and the fly likes Daemon Thread Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Daemon Thread" Watch "Daemon Thread" New topic
Author

Daemon Thread

bhaswar goswami
Greenhorn

Joined: Jul 04, 2005
Posts: 14
hey guys ,
can someone tell me :
1. wht is daemon thread ??
2. how to use it ??
3. wht is the purpose of it


Regards,

Bhaswar Goswami
SCJP 5.0 , SCDJWS


Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

When the only threads running in a JVM are daemon threads, the JVM will exit. That's the whole definition -- there's nothing more to it than that. They're used for background tasks that should run until the main program exits; for example, a thread that continuously checks pagination in a word-processor.


[Jess in Action][AskingGoodQuestions]
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
Because a daemon thread may exit at any point in its code, you have to be careful what types of things it does.

For example, it is probably a bad idea for a daemon thread to do file-writing. The thread might exit at any point during the write, leaving the file in a part-written, corrupt state. Unless the file is a temporary file used only during a single run of the application, you don't want that.


Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18916
    
  40

Originally posted by Peter Chase:
Because a daemon thread may exit at any point in its code, you have to be careful what types of things it does.

For example, it is probably a bad idea for a daemon thread to do file-writing. The thread might exit at any point during the write, leaving the file in a part-written, corrupt state. Unless the file is a temporary file used only during a single run of the application, you don't want that.


You could create some kind of lock. If there is a critcal section of code, ran by a daemon thread, it can lock the VM to prevent it from exiting during this critical section. This will allow background saves to be done by daemon threads.

BTW, there is no black magic here. To prevent the JVM from exiting, just create another user thread -- which simply waits til it is told to complete.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
Originally posted by Henry Wong:


You could create some kind of lock. If there is a critcal section of code, ran by a daemon thread, it can lock the VM to prevent it from exiting during this critical section. This will allow background saves to be done by daemon threads.

BTW, there is no black magic here. To prevent the JVM from exiting, just create another user thread -- which simply waits til it is told to complete.

Henry


That's an interesting idea that I had not thought of.

I would have done it the other way around. If my daemon thread needed to do some file writing (or similar critical task), I would spawn a non-daemon thread to do that work, and have the daemon thread wait for it to finish. I think that the result would be pretty much the same.

Any reason why one or other approach is better?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18916
    
  40

Originally posted by Peter Chase:

That's an interesting idea that I had not thought of.

I would have done it the other way around. If my daemon thread needed to do some file writing (or similar critical task), I would spawn a non-daemon thread to do that work, and have the daemon thread wait for it to finish. I think that the result would be pretty much the same.

Any reason why one or other approach is better?


Just personal preference... By doing this, I encapsulated all the thread stuff in the "lock" class. And never had to worry about it again. I just reused the class.

Henry
karthikeyan Chockalingam
Ranch Hand

Joined: Sep 06, 2003
Posts: 259
By default (unless the parent Thread is a daemon) threads are not daemon, that is isDaemon() method returns false. Hence to set the Thread as Daemon, use the method setDaemon(true) before starting the thread. If setDaemon is invoked after the thread is started it throws IllegalThreadStateException.


http://www.skillassert.com


 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Daemon Thread