Win a copy of Micro Frontends in Action this week in the Server-Side JavaScript and NodeJS forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

Daemon Threads

 
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 ]
 
author and iconoclast
Posts: 24203
43
Mac OS X Eclipse IDE Chrome
  • 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.
 
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.
 
Hot dog! An advertiser loves us THIS much:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic