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.
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.
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
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
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop