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