aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Daemon thread question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Daemon thread question" Watch "Daemon thread question" New topic
Author

Daemon thread question

Yan Bai
Ranch Hand

Joined: Jul 21, 2002
Posts: 125
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


SCJP 1.4
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Maybe this will help:
Beware the Daemons
-Barry


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Anthony Villanueva
Ranch Hand

Joined: Mar 22, 2002
Posts: 1055
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

Joined: Aug 03, 2002
Posts: 7729
Tony means
Yan Bai
Ranch Hand

Joined: Jul 21, 2002
Posts: 125
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

Joined: Jun 04, 2002
Posts: 133
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

Joined: Mar 22, 2002
Posts: 1055
Beware the Graemlins too!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Daemon thread question
 
Similar Threads
what are daemon threads?
Deamon thread how to
thread
Thread
daemon Vs User Thread