File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Daemon and non deomon threads? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Daemon and non deomon threads?" Watch "Daemon and non deomon threads?" New topic

Daemon and non deomon threads?

sonir shah
Ranch Hand

Joined: Nov 01, 2001
Posts: 435
what are deomon and non deomon threads.
I have come across a few questions which say abt these threads.
what do they do?
Raji Addepalli
Ranch Hand

Joined: Dec 05, 2001
Posts: 50
hi sonir!
Some threads should keep running as long as the the program is running such threads are called deamon threads.
Ex:Garbage collector.
A Java program will terminate when all the Non-deamon threads are dead.A thread is said ti die,when it comes out of the run method.A thread may dies,but still the thread Object can exist.Understand the difference keeping in view the definition we had earlier.It is not possible to creat another threadfrom the Thread Object.
Adeamon thread is automatically a deamon thread, in fact we program thread to be a deamon thread .If the code in the thread comes out of run()method it may die,even if it is specified to be a deamon thread.
A deamon thread can be created by Calling setDeamon() method in the thread class.However this should be done before execution of the thread starts.
[ January 10, 2002: Message edited by: Raji Addepalli ]
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Originally posted by sonir shah:
what are deomon and non deomon threads.
I have come across a few questions which say abt these threads.
what do they do?

In Java, there are two types of threads, generally known as daemon threads and user threads. The difference between them relates to how they are managed and terminated. A user thread (which is the default type of thread you get when you create a new one) will run until it is completed. A daemon thread is a thread which will be terminated when all user threads have terminated. Therefore, daemon threads are usually useful as background processes that you'd like to have running as long as your application is running. When the user quits the application (and all user threads have been terminated) any daemon threads you have running will simply be killed.
There is actually another node which has brought up a lot of information about daemon threads and user threads that you can find here.

SCJP Tipline, etc.
I agree. Here's the link:
subject: Daemon and non deomon threads?
It's not a secret anymore!