Daemon Thread is the thread in java which is generally created by JVM and is doing some tasks in the background like handling requests or various cronjobs that can exist in an application. These are service providing threads.
Some Important Points -:
1. Daemon Thread is basically service providers thread. Threads that work in the background and perform background task to support or provide the service to another thread in runtime environment are called daemon threads. These are basically created and terminated by the JVM as soon as user thread finish there execution.
These are very useful for supporting tasks and are only needed while normal threads are executing. If normal threads are not running and remaining threads are daemon threads, then JVM kills all that Thread.
2. For Example - The thread that processes mouse events for a Java program is a daemon thread, the garbage collector thread that also provides a service to remove all the anonymous object from the program also are the daemon threads. The JVM exits whenever all non-daemon threads have completed.
3. Generally, threads that run application code are not daemon threads and threads that run system code are daemon threads. If a thread dies and there are no other threads except daemon threads alive, the Java Virtual Machine (JVM) stops working.
Methods Used in Daemon Thread -:
There are two methods used for daemon Thread -
1. public final void setDaemon(boolean isDaemon) - This method is used for making a user thread to Daemon thread or vice versa
2. public final boolean isDaemon() - This method is used for checking the status of the thread.
By default, No any thread we create is a daemon thread. However, we can use the setDaemon(true) method to turn it into one.
Summary in Brief - :
1. Daemon Thread is Service provider thread.
2. They provide services to other thread.
3. They always run in the background.
4. They never show any output.
5. They will be dead automatically whenever the thread to whom they were providing a service is dead.