Java / Java Concurrency and Multi Threading in Java 17 and Java 21 Interview questions
What is a daemon thread in Java?
A daemon thread is a background thread that the JVM does not wait for when deciding whether to exit. Once every non-daemon (user) thread has finished, the JVM shuts down immediately, even if daemon threads are still running, terminating them abruptly.
Common examples include the garbage collector's threads and finalizer threads that run for the lifetime of the application, doing housekeeping work rather than user-visible tasks.
You mark a thread as a daemon by calling setDaemon(true) before it is started; calling it after start() throws IllegalThreadStateException. A thread created from an existing daemon thread inherits daemon status by default.
Because daemon threads can be killed mid-operation with no cleanup, they shouldn't be used for work that must complete reliably, such as writing to a file or a database.
More Related questions...