• 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
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

setDaemon method

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
setDaemon(true);
What does this statement do in Multithreading scenario.
 
Ranch Hand
Posts: 1228
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please have a look at this thread.
[ November 09, 2005: Message edited by: Srinivasa Raghavan ]
 
Arnab Saha
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please have a look at the code:

public class A extends Thread {
A() {
setDaemon(true);
}

public void run() {
(new B()).start();
try {
Thread.sleep(60000);
} catch (InterruptedException x) {}
System.out.println("A done");
}

class B extends Thread {
public void run() {
try {
Thread.sleep(60000);
} catch (InterruptedException x) {}
System.out.println("B done");
}
}

public static void main(String[] args) {
(new A()).start();
}
}
*************
No o/p is printed

But if in the sleep calls 60000 is replaced by 10
We get:
A done
B done.
Can u explain it?
 
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Daemon threads are a kind of threads which provide services to other user(non-daemon) threads. When there is no live user threads, then JVM will abruptly kill the daemon threads. As log as the pgm has some live threads, daemon wont be killed abruptly. In your pgm, main thread is the user thread, it will end as soon as it starts. Hence the JVM kills the other daemon threads immly after the ending of main thread.

Can you make one of your thread, preferably B as non -daemon by calling setDaemon(false) inside the run method.
 
Lakshmanan Arunachalam
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Sample 2:

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic