File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes What exactly are daemon threads? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "What exactly are daemon threads?" Watch "What exactly are daemon threads?" New topic
Author

What exactly are daemon threads?

Chiran Mathur
Ranch Hand

Joined: Feb 07, 2001
Posts: 63
I am having trouble understand the concept of daemon threads. Can a user explicitly start these? Can someone explain in detail?
praveena kuppachi
Greenhorn

Joined: Jan 24, 2003
Posts: 15
The JVM categorizes Threads into two groups :
user and daemon.
when initially created threads are user threads,but before they are started they may be tagged as daemon threads by the setDaemon method.This tag becomes important when the JVM tries to decide when to shut down a program .
Every time a thread dies,The JVM looks at the remaining threads to see whether any of them is a user thread.If only daemon threads are left,the JVM terminates.
Hope Iam clear.
Sarma Lolla
Ranch Hand

Joined: Oct 21, 2002
Posts: 203
Daemon thread are basically used for services. They serve other thread's requirements. If a thread group is a daemon thread group then all the threads created with that thread group default to daemon threads. SetDaemon() method is also available on thread group to make any thread group a daemon thread group.
Any exception that is raised in daemon threads are ignored by JVM.
Please correct me if any thing I mentioed here is wrong. My knowledge is based on reverse enginnering from Mocks.
Chiran Mathur
Ranch Hand

Joined: Feb 07, 2001
Posts: 63
So, If understand it correctly , a daemon threda has to be explicityly set by the developer. On it's own none of threads sstarted by the system are daemon threads?
Am I correct in assuming that?
Ambapali Pal
Ranch Hand

Joined: Dec 17, 2002
Posts: 47
So, If understand it correctly , a daemon threda has to be explicityly set by the developer. On it's own none of threads sstarted by the system are daemon threads?
If one daemon thread creates another thread then that will also be a daemon thread.
watch for the following from Thread API

Every thread has a priority. Threads with higher priority are executed in preference to threads with lower priority. Each thread may or may not also be marked as a daemon. When code running in some thread creates a new Thread object, the new thread has its priority initially set equal to the priority of the creating thread, and is a daemon thread if and only if the creating thread is a daemon.

------------------
Every time a thread dies,The JVM looks at the remaining threads to see whether any of them is a user thread.If only daemon threads are left,the JVM terminates.
You are right. So there is no guarantee that the Daemon thread will get a chance to run. The life of Daemon depends on the user thread of the program.
Ambapali
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
So, If understand it correctly , a daemon threda has to be explicityly set by the developer. On it's own none of threads sstarted by the system are daemon threads?
No, the system can start threads on its own for various reasons, which may be daemon threads. The main example I know of is garbage collection - it's performed by a low-priority daemon thread started by the system without any explicit command. (Unless GC is caused by System.gc() or an impending OutOfMemoryError, in which case it's probably being done by the same thread that called gc() or attempted to allocate new memory.)
Any thread started by the programmer will be a non-daemon thread, unless (a) it's explicitly set to be a daemon thread, or (b) it's created from within another daemon thread.


"I'm not back." - Bill Harding, Twister
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: What exactly are daemon threads?
 
Similar Threads
Doubts in threads !
daemon threads
Daemon Thread
User and Daemon threads
Priority of Daemon threads