File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Daemon threads & non daemon Threads

 
Anonymous
Ranch Hand
Posts: 18944
  • 0
  • send pies
  • Quote
  • Report post to moderator
what is a daemon thread ? whats the difference between the daemon thread & non daemon thread?
I just want the explanation.
 
Frank Carver
Sheriff
Posts: 6920
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The strict difference is that a daemon thread is one which won't prevent the program from ending. This may not be a very useful way to describe the difference, though, so here's an example:
Imagine a Java application. It starts off with just one thread, the "main" thread which runs the main method. This is a regular (non-daemon) thread. Now imagine again that the program starts another thread which sits in a loop doing something. If the new thread is a regular (non-daemon) thread, it will prevent the program from finishing when "main" ends, and keep going forever!

Now, this isn't always what is required. Sometimes you want to end this "background" processing when the program finishes. To do this, you can mark threads as daemon threads. Daemon threads don't prevent the program from ending, but stop when the main thread, stops:

Try running the two different classes above, and see how the
output is different.
A classic example of a daemon thread is the garbage collector thread found in many Java Virtual Machines. It needs to run continuously while any other threads are running, but should not prevent the program from exiting. When the program exits, there is no more need for a garbage collector.
 
prasad bh
Greenhorn
Posts: 1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Daemon thread is a thread, which runs only when it is invoked to do the task. It is a thread which has lower priority and if a higer priority thread comes, Daemon thread's execution is stopped and resources are given to higer one. for ex: garbage collection thread.
:-)
Prasad Bh.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic