File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Daemon Thread Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Daemon Thread" Watch "Daemon Thread" New topic
Author

Daemon Thread

MannY Gates
Ranch Hand

Joined: Nov 05, 2004
Posts: 57
In the below code I have created one daemon thread and one user thread.
According to my understanding the daemon thred should die along with the main thread (Since it was created by main thread). This is not happening when I compile and run the below program. The daemon thread continues it's execution along with the user thread.

Any thoughts???




Regards,<br /> <br />MannY<br />----------------------------------------------------------- <br />"Beam me up Scotty, there's no intelligent life down here" !!! <br />------------------------------------------------------------
Richard Vagner
Ranch Hand

Joined: Jun 26, 2001
Posts: 107
I think a daemon thread dies when there are no more user threads running. Even when main thread( a user thread) dies, a user thread (Threadtwo)spawned from main thread is still alive. If you set Threadtwo to daemon thread, the program will stop because when main thread finishes there'll be no user threads left.
Swapnil Sapar
Ranch Hand

Joined: Sep 29, 2003
Posts: 41
Hey,
In your code, both the threads are sleeping for same time. Try with the different sleep time, like the non daemon thread should exit before the daemon thread's sleep is over.

Try the code below.



SCJP 1.4 (90%), SCWCD 1.4 (92%)
wonderprat shah
Greenhorn

Joined: May 30, 2004
Posts: 6
According to me the necessary condition for the daemon thread to survive is the presence of any of the user thread or the parent thread(which is main thread in your program).

So since in ur example, one user thread is alive, ur daemon thread will survive on that... even though main thread is not alive...

Correct me if my understanding is wrong.

Regards,
Pratik
Richard Vagner
Ranch Hand

Joined: Jun 26, 2001
Posts: 107
What is the point of setting different sleep time? It is not like the thread will hang there for ever. It will eventually complete. Try setting the sleep time at 1000, you will see the result faster. I think the point here is when main thread dies a child thread may still be alive until it completes mission. At that point JVM shuts down.

To make sure the application stops when main thread dies, you will need set all threads spawn by main threads to daemon.
wonderprat shah
Greenhorn

Joined: May 30, 2004
Posts: 6
but does a daemon thread survive if there are no user threads alive....

I mean, if the main thread which has spawned the daemon thread is dead, & there are no other user threads alive, then can the daemon thread still complete its execution or will it die before that...

Regards,
Pratik
Richard Vagner
Ranch Hand

Joined: Jun 26, 2001
Posts: 107
No, daemon thread will die if there are no user threads running. If we change the code a bit, you will see when user thread dies, the program terminates.


[ December 31, 2004: Message edited by: Richard Vagner ]
MannY Gates
Ranch Hand

Joined: Nov 05, 2004
Posts: 57
Thanks to everyone for pitching in.

To make things sure about the fact that the daemon thread continues it's execution till the presence of any of the user thread, I created the user thread in another hierarchy ( Created thread "Three" as a child of thread "One" and made sure that threa one and two are daemons. )

The result proved that the daemon thread survives till the presence of any of the user thread (and ofcource the thread which creates it).

Can anyone point me to the theoretical resource which states this fact?


Below is the sample code:

Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
"Can anyone point me to the theoretical resource which states this fact?"

The API for method Thread.setDaemon states:
"

The Java Virtual Machine exits when the only threads running are all daemon threads"


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
MannY Gates
Ranch Hand

Joined: Nov 05, 2004
Posts: 57
Thanks Barry
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Daemon Thread