• Post Reply Bookmark Topic Watch Topic
  • New Topic

Daemon Threads  RSS feed

 
Monu Tripathi
Rancher
Posts: 1369
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • 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 ]
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • 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.
 
Ivan Koblik
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!