• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Daemon Threads

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi!
Can anyone explain to me what are the Demon threads and what are their properties and Use.
Is any information is available at some site then please give me the URL.
Thanks
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Daemon Threads are the low priority threads which runs in the background. A thread can be made by calling the method
setDaemon(boolean true)
Daemon threads runs, whenever system give resources to it. But there is no fixed schedule or rule set by the scheduler for it. eg.-Garbage collector is a daemon thread.
HTH
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually, when will we set a thread to be a daemon thread?
I find that when all the non-daemon thread ends, even the daemon
thread is still running, the JVM will also stop.
<code>
public class hx implements Runnable {
public void run() {
try {
Thread.currentThread().sleep(10000);
}
catch(InterruptedException e) {}
for (int i = 0; i < 10; i++)
System.out.println(i);
}
public static void main(String a[]) {
try {
Thread.currentThread().sleep(5000);
}
catch(InterruptedException e) {}

Thread t = new Thread(new hx());
t.setDaemon(true);
t.start();
}
}
</code>
 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi
Setting a thread to be deamon thread (bycalling setDeamon(true))is to be done before calling start() on that thread. These deamon threads exist for serving user threads. When there are no more user threads, deamon threads have no reason to exist. So JVM exits when threads left are only deamon threads.
Shrinivas
 
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A good article which explains what daemon threads are can be found at:
Beware the daemons
 
Rakesh Sharma
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got it.
Thanks friends.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic