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

 
Rancher
Posts: 1369
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am trying to understand the difference between Daemon threads and user-level threads.As i gather, the main thread will exit irespective of whether there are any daemon threads running. Main only waits for user-level threads to complete(please let me know if this is wrong).

To understand this, i wrote a code fragment as:


When i run this code, i get the following output :



the last statement shouldnt have figured in the output. That being said, when i increase the sleep time of the daemon thread to a "sufficiently" large value(say 100 ms), i get the expected output:


Can anyone reason why i am getting the statement in bold(Exiting Daemon Thread ...) during my first run(code inline)?
[ December 03, 2008: Message edited by: Monu Tripathi ]
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Monu Tripathi:

Can anyone reason why i am getting the statement in bold(Exiting Daemon Thread ...) during my first run(code inline)?



It's really nothing complicated. The JVM will terminate when only daemon threads are running. Terminating takes a non-zero amount of time, so in this case, while the JVM is working on terminating (or perhaps even before the JVM has even noticed that it should terminate) your daemon thread prints its final message.

Also, the thing to remember when learning about threads is that the concept of exact ordering of events across threads is meaningless, unless the threads are deliberately coordinating their work by synchronizing on the same object.
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Monu,

As I see it, your test proves only that daemon threads are killed when there is no more other threads alive.

Daemon thread, as any other kind, dies if it exits the run() method. So it is the developer's responsibility to keep it alive.

In the first test, program was running long enough to allow the daemon thread finish its execution.
[ December 03, 2008: Message edited by: Ivan Koblik ]
 
Monu Tripathi
Rancher
Posts: 1369
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

It's really nothing complicated. The JVM will terminate when only daemon threads are running. Terminating takes a non-zero amount of time, so in this case, while the JVM is working on terminating (or perhaps even before the JVM has even noticed that it should terminate) your daemon thread prints its final message.


I probably guessed this; just wanted an official's concurrence
Alos, many thanks for your advice.
 
Monu Tripathi
Rancher
Posts: 1369
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

As I see it, your test proves only that daemon threads are killed when there is no more other threads alive.


I tested the other clause of the rule(user level threads are allowed to complete) by interchanging the sleep times and it worked as expected. User-level threads were allowed to complete.

Thanks Ivan for your answer and time.
 
Water proof donuts! Eat them while reading this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic