aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Aboutt Daemon Threads Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Aboutt Daemon Threads" Watch "Aboutt Daemon Threads" New topic
Author

Aboutt Daemon Threads

Ghanashyam Apte
Greenhorn

Joined: May 12, 2001
Posts: 4
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

Joined: Mar 13, 2001
Posts: 276
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

Joined: May 12, 2001
Posts: 4
Thanx
But my question is how can a daemon thread die before user thread dies?
Ghanashyam
Daniel Dunleavy
Ranch Hand

Joined: Mar 13, 2001
Posts: 276
As I said....when run() is done.
Mikael Jonasson
Ranch Hand

Joined: May 16, 2001
Posts: 158
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

Joined: Feb 14, 2001
Posts: 234

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.

sona<br />SCJP
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
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
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Aboutt Daemon Threads