• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Daemon Thread

 
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
 
author and iconoclast
Posts: 24203
43
Mac OS X Eclipse IDE Chrome
  • 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.
 
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.
 
author
Posts: 23879
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux 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
Posts: 23879
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux 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
 
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.
 
Rototillers convert rich soil into dirt. Please note that this tiny ad is not a rototiller:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic