• Post Reply Bookmark Topic Watch Topic
  • New Topic

how Daemon Threads are working exactly???why Daemon??  RSS feed

 
basha khan
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
plz tell me the exact behavior of Daemon Threads.
i know that the main Thread is not Daemon.user Threads are also not untill explicitly specified.
so in a comman application, where the Daemons stands??.plz tell an exact situation where the Daemon threads are usefull.
wishing everybody...
 
Roy Ben Ami
Ranch Hand
Posts: 732
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi !
First of all you need to understand, like you said, the your program will run as long as there is an active non-daemon thread running.
It means that if u make extra threads , your program will still run even if the main method ends until all other threads you made are finished.
BUT, you can make your threads daemon, meaning that when the main thread exits your program will also exits and stops the daemon threads.
this is useful if u need to make threads that do background job in your application (like taking care of resources etc) and you want them not to be able to keep your program alive by themselves.
Hope i made it clearer.
 
Ragu Sivaraman
Ranch Hand
Posts: 464
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here you go
Any Java thread can be a daemon thread. Daemon threads are service providers for other threads or objects running in the same process as the daemon thread. For example, the HotJava browser has a daemon thread, named Background Image Reader, that reads images from the file system or the network for any object or thread that needs an image.
Daemon threads are typically independent threads within an application that provide services for other objects within that same application. The run() method for a daemon thread is typically an infinite loop that waits for a service request.
When the only remaining threads in a process are daemon threads, the interpreter exits. This makes sense because when there are only daemon threads remaining, there is no other thread for which a daemon thread can provide a service.
To specify that a thread is a daemon thread call the setDaemon() method with a boolean parameter that is true. To determine if a thread is a daemon thread use the accessor method isDaemon().
Ragu
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
basha,
We'd like you to read the Javaranch Naming Policy and register again.
Thank you for your cooperation
 
basha khan
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank u ami&ragu doubt was over.
wishing everyone a splendid day.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!