Are Java daemon threads automatically killed when their parent exit terminates?

If Thread creates a daemon thread, can I rely on the fact that when the parent exits the run method, will the son also complete?

+3
source share
3 answers

No - threads are independent. It makes no sense for one thread to “own” another and force it to complete.

If you really ask if all non-daemon threads in the application have died, you can rely on the process of dying: yes, you can. But you can rely on all of this.

In particular, if there are two non-daemons, each of which created a daemon thread, and one of the non-daemon threads terminates, then the other three threads will continue to work.

+9

, JVM, .

+1

C: http://journals.ecs.soton.ac.uk/java/tutorial/java/threads/daemon.html

When the remaining threads in the process are daemon threads, the interpreter exits. This makes sense, because when only daemon threads remain, there is no other thread for which the daemon thread can provide the service.

0
source

Source: https://habr.com/ru/post/1708602/


All Articles