What are Java daemon threads and when to use them?

Asked

Viewed 2,831 times

2

From the Java documentation I follow the following excerpt:

The Java Virtual Machine exits when the only threads running are all daemon threads.

But it’s still unclear to me when to set a thread as daemon or not. Would anyone have a clearer definition, preferably with an example? Thank you!

1 answer

2


Daemon threads are interrupted when the thread main, the one that executes the method main, ends running and the program ends.

If a thread nay is daemon, the Java process remains active and running, even when it reaches the end of the method main and the thread main ends.

By default, the threads inherit the property of being daemon of thread that created them. Like the thread leading nay is daemon, commonly the threads created in the program are also not.

This makes it common programs that "do not end", mainly because few understand this concept or forget to create a mechanism to end threads created during the implementation of the main programme.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.