0

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?

Community
  • 1
  • 1
Nargis
  • 4,687
  • 1
  • 28
  • 45

3 Answers3

1

A daemon thread is ended if one of these two conditions becomes true:

  • The thread returns from the run() method
  • The virtual machine is terminated

To 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.

Jan Henke
  • 875
  • 1
  • 15
  • 28
1

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.

Vineet Kasat
  • 994
  • 7
  • 14
0

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

Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275