I have a fair idea about what is Daemon Thread
But I want to know whether we can kill a Daemon Thread or when does a Daemon Thread get killed in Java?
I have a fair idea about what is Daemon Thread
But I want to know whether we can kill a Daemon Thread or when does a Daemon Thread get killed in Java?
A daemon thread is ended if one of these two conditions becomes true:
run()
methodTo actively end a (daemon) thread the most common method is to signal the thread the request to have it terminated, the thread should check for this request in regular intervals and end itself once such a request has been made.
Daemon thread is the thread which runs in background. These threads are started by defalt by JVM. Also we can start a daemon thread through a program as well.
When a main program starts, the only non-daemon thread which is started is the main thread, rest (GC ets) are daemon.
These thread get killed automatically when there is no non-daemon thread running as JVM kills itself after that.
The idea abour daemon threads is that when the last non-daemon thread ends the application ends too. Daemon threads cannot keep the JVM running