| Author |
Daemon Thread
|
Akhilesh Trivedi
Ranch Hand
Joined: Jun 22, 2005
Posts: 1493
|
|
|
What is a daemon thread? How does it differ from a non-daemon thread...
|
Keep Smiling Always — My life is smoother when running silent. -paul
[FAQs] [Certification Guides] [The Linux Documentation Project]
|
 |
Sherry Jacob
Ranch Hand
Joined: Jun 29, 2005
Posts: 128
|
|
Originally posted by Akhil Trivedi: What is a daemon thread? How does it differ from a non-daemon thread...
Well, whenever a standalone application is started, a user thread is automatically created which executes the main() method. All other threads are spawned from the main() mehtod. After spawning child threads, the main() method can finish but the program will keep running unless all child threads complete. However, if a thread is declared as daemon, it ensures that the thread will be terminated as soon as the main() method terminates. Basically daemon threads are there only 2 serve user threads. Hope this helps !! Cheers !!
|
<strong><br />Cheers !!<br /> <br />Sherry<br /></strong><br />[SCJP 1.4]
|
 |
Stan James
(instanceof Sidekick)
Ranch Hand
Joined: Jan 29, 2003
Posts: 8791
|
|
|
The JavaDoc is confusing doublespeak, something about "the program can end when all threads that are not daemon have ended." It doesn't explicitly say that it doesn't care about threads that are daemon but the program can end whether there are daemon threads or not.
|
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
|
 |
Philip Heller
author
Ranch Hand
Joined: Oct 24, 2000
Posts: 119
|
|
Originally posted by Akhil Trivedi: What is a daemon thread? How does it differ from a non-daemon thread...
I don't completely hate the name, because it harks back to the early days of UNIX, when the terminology was ... um ... colorful. There's a thought-experiment in physics called "Maxwell's daemon", where Maxwell imagined an invisible daemon sitting at the mouth of a cave, letting only hot molecules in and cold molecules out. So the cave gets hotter and hotter. Maxwell was educated in the 19th century, hence the odd spelling. So a daemon is a mysterious and invisible thing. A Java application has two kinds of thread. First there's the visible and evident kind, known as user threads. When your application starts up, the JVM creats one such thread and tells it to call your main() method. Any other threads spawned off by this thread are also user threads. Meanwhile, sneaking around behind the scenes on your behalf, working mysteriously and invisibly, are daemon threads. The big ones are the garbage collector and the event-andling threads. You don't create them, you don't control them, you don't have to think about them very much. And they don't ever die, because your program can't survive without them. The JVM decides it's time to be done when all of your user threads are done. If daemon threads are running, that's ok: that's just infrastructure. -- Phil
|
Consultant to SCJP team.<br />Co-designer of SCJD exam.<br />Co-author of "Complete Java 2 Certification Study Guide".<br />Author of "Ground-Up Java".
|
 |
Ilja Preuss
author
Sheriff
Joined: Jul 11, 2001
Posts: 14112
|
|
|
Moving to Threads and Synchronization...
|
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
|
 |
Stan James
(instanceof Sidekick)
Ranch Hand
Joined: Jan 29, 2003
Posts: 8791
|
|
|
Daemon threads are not exclusively the domain of JVM infrastructure, though that may be the most common use. We can make our own (set deamon true on a thread before we start it) if we don't care about shutting it down at the end of the program. Say I have a thread that periodically checks a directory to see if there are any new files. If I make that a daemon I can shut down the main user thread and the deamon just goes away. If I don't make it a daemon I have to be sure to terminate it before exiting the program.
|
 |
Mr. C Lamont Gilbert
Ranch Hand
Joined: Oct 05, 2001
Posts: 1170
|
|
|
User threads make your program keep running as long as one of them is running. Daemon threads do not keep your program from exiting. Thus daemon threads can be rudely interrupted by an exiting program.
|
 |
 |
|
|
subject: Daemon Thread
|
|
|