What is a daemon thread?
Daemon threads are service threads.Their main objective is to provide different services to other thread.They normally runs in an infinite loop and attain client thread’s requesting service when no other thread is present or exists.The Daemon thread automatically exists.
It works without having any connection with overall state of the program,like Garbage collector.
The application threads are not daemon threads but the system threads are daemon threads.
If a thread dies and there are no other threads to run, daemon threads remains alive until java virtual machine stops. JVM does not wait for the death or destroy of Daemon thread before concluding a program.
If a daemon attribute is not explicitly specified,the thread inherits the daemon attribute of its parent thread.
Here is the code to create a Daemon thread
Thread t=new Thread(); //creates a new thread t.setDaemon(true); //mark the thread as daemon thread
Methods of Daemon thread
By default,all threads created by Daemon thread are also Daemon thread.
Below are the methods supported by a Daemon thread:
|getName()||String||returns the nae of the ThreadGroup|
|setName()||void||Sets the name of the ThreadGroup|
|getParent()||ThreadGroup||returns the parent ThreadGroup of the thread or ThreadGroup|
|getMaxPriority()||int||Returns the maximum priority of the ThreadGroup|
|activeCount()||int||Returns the active thread of a ThreadGroup|
|activeGroupCount()||int||Returns the number of active ThreadGroups in the ThreadGroup|
|isDaemon()||boolean||Returns true if the thread is a daemon thread else returns false|
|setDaemon()||void||Sets the thread as a daemon thread|