Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Daemon Thread

 
bhaswar goswami
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey guys ,
can someone tell me :
1. wht is daemon thread ??
2. how to use it ??
3. wht is the purpose of it
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Henry Wong
author
Marshal
Pie
Posts: 21112
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21112
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 259
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic