Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Aboutt Daemon Threads

 
Ghanashyam Apte
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Everybody,
My understanding of Daemon thread is that daemon threads are stopped when all user threads die.But it doesn't seem to happen in the following program:
Daemon Thread t1 dies even before main (user) thread dies.Somebody plz explain.
class Th3 extends Thread {
int currentValue = 0;
Th3(String nm){
super(nm);
}
public void run(){
while(currentValue<5){
System.out.println(getName()+": "+(currentValue++));
}
try{
Thread.sleep(100);
}catch(InterruptedException ie){System.out.println(ie);}
System.out.println("Exiting run()");
}

}
class Client3{
public static void main(String args[]){

Th3 t1 = new Th3("Thread1");
t1.setDaemon(true);
t1.start();
System.out.println(t1.isDaemon());
try{
Thread.sleep(500);

}catch(InterruptedException ie){System.out.println(ie);}

System.out.println(t1.isAlive());

System.out.println("Exiting main()");

}

}

/*
The O/p on my machine is as follows:
true
Thread1: 0
Thread1: 1
Thread1: 2
Thread1: 3
Thread1: 4
Exiting run()
false
Exiting main()
*/
Ghanashyam
 
Daniel Dunleavy
Ranch Hand
Posts: 276
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not positive of this but I think....
Thread dies when its run() is done or if user threads die. In otherwords you can't have a daemon running with no user threads.
Dan
 
Ghanashyam Apte
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanx
But my question is how can a daemon thread die before user thread dies?
Ghanashyam
 
Daniel Dunleavy
Ranch Hand
Posts: 276
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As I said....when run() is done.
 
Mikael Jonasson
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure about this, but aren't deamonthreads supposed to be eternal?
Thus, what happends is that when you start the new thread, both threads (the user and the daemon) has the same priority. You then put the userthread to sleep, thus enabling the daemonthread to run. It will then run a few times, sleeping less then the mainthread does, thus never giving control back to the mainthread.
Try to increase the limit of your while (say 100 instead of 5) and see if you get the same result...
/Mike
 
sona gold
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

My understanding of Daemon thread is that daemon threads are stopped when all user threads die.But it doesn't seem to happen in the following program:
Daemon Thread t1 dies even before main (user) thread dies.Somebody plz explain.
daemon threads are stopped when all threads die.
but only if the daemon threads are still running when the other threads are dead.
here the dameon thread is dying before other threads there is no problem in that
but if the other threads had died and the daemon thread would still be running it would be forced to die.
 
Dave Vick
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think there is some confusion here. It sounds like some people are gettng two related facts mixed up.
From the Sun tutorial...
Stopping a Thread
A program doesn't stop a thread like it stops an applet (by calling a method). Rather, a thread arranges for its own death by having a run method that terminates naturally.

This would apply to daemon threads too. So, yes, the t1 thread is dying before the other thread because its run method was finished.
Also, a program will terminate as soon as there are no non-daemon threads left running (like the grabage collector).
to test this try the following:
have your daemon thread sleep for a couple of seconds and your main thread not sleep at all - the program should end before the daemon prints all of it numbers.

hth
Dave
 
Dave Vick
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry,
Just ran this myself to make sure I wasn't giving anyone bad scoop and I didn't notice that the daemon thread doesn't sleep until after it is done printing out its numbers. Trying what I said in my first post will still illustrate the daemon dying when the main thread does but it will print out all numbers before it dies it wont print its last message Exiting run().
Sorry if that confused anyone.
Dave
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic