Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Daemon thread question

 
Yan Bai
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For non-daemon thread, the program, say - the thread runs main(), will not terminate until all threads complete. But for daemon, since it is supposed to run in the background of one program, the program can terminate once the main() finishes its job, even those daemon threads are still active.
My question is,
Assume a daemon thread has an infinite loop, will it continue running after the main() finished? So it *seems* that the program is finished, but indeed, it's not really done yet. One needs to kill the process manually sometime later when he noticed this, is this a potential hole?
What are daemon threads use for then? :roll:
Thanks very much
-Yan
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe this will help:
Beware the Daemons
-Barry
 
Anthony Villanueva
Ranch Hand
Posts: 1055
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Yan Bai:
Assume a daemon thread has an infinite loop, will it continue running after the main() finished?


It wouldn't.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tony means
 
Yan Bai
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the help!
Just want to confirm - Does this mean that both user threads and daemon threads will terminate once the main program terminates?
 
Paul Villangca
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Yan Bai,
There's nothing special about the 'main program' thread, it's just another user thread. That being said, the Java runtime terminates only if there are no more user threads running, the main() method among them.
Hope this helps,
Paul Villangca
 
Anthony Villanueva
Ranch Hand
Posts: 1055
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Beware the Graemlins too!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic