• 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 Thread

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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???


 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:

 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"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"
 
MannY Gates
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Barry
 
reply
    Bookmark Topic Watch Topic
  • New Topic