• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Daemon Thread

 
Akhilesh Trivedi
Ranch Hand
Posts: 1603
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is a daemon thread? How does it differ from a non-daemon thread...
 
Sherry Jacob
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 !!
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Philip Heller
author
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to Threads and Synchronization...
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1170
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic