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
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.
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".
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
Joined: Jan 29, 2003
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.
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.