Week 114 — What is a daemon thread?
Question of the Week #114
What is a daemon thread?
2 Replies
Threads allow executing multiple operations in parallel. Once all threads terminate, the application is shut down. An exception to this rule are daemon threads which don't prevent the application from stopping. When all non-daemon threads have finished, the JVM initiates its shutdown logic and stops all daemon threads no matter what these are currently doing.
The following example creates two threads, one of which being a daemon thread. The daemon thread sleeps 3 seconds while the non-daemon thread only sleeps for one second:
When running this example, it can be seen that both threads start and that the non-daemon thread finishes after one second. Once that happens, the JVM stops and the daemon thread is killed without printing the text
daemon thread finished
or daemon thread interrupted
.
📖 Sample answer from dan1st
Daemon thread is a thread which destroys after all the thread.. even the last priority threads will destroy before the daemon thread
Submission from aarvy