Quick revision
The short answer
A daemon thread is a background thread that does not keep the JVM alive: the JVM exits as soon as every non-daemon thread has finished, whatever the daemon threads are doing. We create one by calling setDaemon(true) before start(), and a new thread inherits the daemon status of the thread that creates it. When the JVM exits, daemon threads are simply abandoned, so their finally blocks do not run and unflushed buffers are lost. Virtual threads are always daemon threads.
What to remember
In this answer, we saw that the JVM exits when the last non-daemon thread finishes and does not wait for daemon threads. We create a daemon with setDaemon(true) before start(), since calling it afterwards throws IllegalThreadStateException, and new threads inherit the status of their creator. At exit, daemon threads are abandoned without running finally blocks or flushing buffers, so they suit only work that may be lost. Virtual threads are always daemons, so we must join them or close their executor.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
A daemon thread is a background thread that does not keep the JVM alive: the JVM exits as soon as every non-daemon thread has finished, whatever the daemon threads are doing. We create one by calling setDaemon(true) before start(), and a new thread inherits the daemon status of the thread that creates it. When the JVM exits, daemon threads are simply abandoned, so their finally blocks do not run and unflushed buffers are lost. Virtual threads are always daemon threads.
Why this question is asked
Daemon threads decide whether a program ends at all, and whether it ends cleanly. A non-daemon thread forgotten in a library can stop a batch job from ever exiting, and a daemon thread doing real work can lose data when the JVM leaves. Interviewers ask about them to see whether we understand JVM shutdown, which matters for command-line tools, tests and containers. In this answer, we will recap when the JVM exits, create a daemon thread, see the inheritance rule and the IllegalThreadStateException, watch what happens to a daemon at exit, and look at virtual threads.
1A Quick Recap: When Does the JVM Exit?
The JVM keeps running while at least one non-daemon thread is alive. The main thread that runs our main method is a non-daemon thread, so are threads we create from it by default, and so are the threads of Executors.newFixedThreadPool(). When the last non-daemon thread finishes, the JVM starts its shutdown sequence: it runs registered shutdown hooks and then halts. The other way to end is an explicit System.exit(), which starts the same sequence regardless of which threads are still running.
A daemon thread is one the JVM does not wait for. The name comes from Unix daemons, background processes that serve others. The JVM's own background threads, such as the garbage collector's helpers and the Reference Handler, are of this kind, because they exist only to serve the program and should not keep it alive.
2Creating a Daemon Thread
We mark a thread as a daemon with setDaemon(true), and it must be done before start(). Once the thread has started, its status is fixed, and trying to change it throws IllegalThreadStateException.
The reason for the rule is that the JVM's exit logic counts live non-daemon threads, so allowing a running thread to switch would make that count unreliable.
3Daemon Status Is Inherited
A new thread starts with the daemon status of the thread that creates it. Threads created from main are non-daemon because main is non-daemon. But a thread created from inside a daemon thread is a daemon too, unless we change it before starting it.
This is why the ThreadFactory used by a pool matters. Executors.defaultThreadFactory() explicitly sets every thread to non-daemon, whatever thread created the pool, while ForkJoinPool.commonPool() creates daemon workers. So a task's survival at exit depends on which pool runs it, not only on how we wrote it.
4What Happens to a Daemon Thread at Exit
When the last non-daemon thread finishes, the JVM does not interrupt daemon threads or ask them to finish. It runs the shutdown hooks and then halts, and the daemon threads stop wherever they are, in the middle of a loop or a write.
- A thread finishesor
System.exit()is called - Any non-daemon threads still alive?YesKeep runningNoBegin shutdown
- Run shutdown hooksthese do run, and can flush or close resources
- Halt the JVMdaemon threads stop mid-instruction, no finally blocks
4 steps in order. Play them, or step through them one at a time.
Notice that neither the try-with-resources close() nor the finally block runs, so the BufferedWriter's last buffer is never flushed. The JVM does not unwind the daemon thread's stack; it simply stops executing it. So daemon threads are only right for work that may be abandoned at any instant, such as a heartbeat, a cache refresher or a metrics sampler. Anything that writes data, sends messages or holds external state should be a non-daemon thread with an orderly shutdown, or should be joined before main returns.
5Virtual Threads Are Always Daemons
Since Java 21, virtual threads are always daemon threads. isDaemon() returns true, and calling setDaemon(false) on one throws IllegalArgumentException. The builder Thread.ofVirtual() has no daemon option at all.
So a program that starts virtual threads from main must wait for them, by joining them or by closing the ExecutorService from newVirtualThreadPerTaskExecutor(), whose close() waits for all submitted tasks. Server applications rarely notice, because the web server keeps non-daemon threads alive until shutdown.
6Follow-up questions
Do shutdown hooks run when only daemon threads remain?
Yes. The JVM starts the normal shutdown sequence, so registered shutdown hooks run. That makes a hook a good place to flush or stop daemon work in an orderly way, as long as it finishes quickly. Hooks do not run on a hard kill such as kill -9.
Is a thread from a fixed thread pool a daemon?
No. Executors.newFixedThreadPool() uses the default thread factory, which makes non-daemon threads. That is why a program that creates a pool and never calls shutdown() does not exit after main returns.
Can we make the JVM wait for a daemon thread?
Yes, by calling join() on it from a non-daemon thread such as main. The daemon flag only matters when the JVM decides whether to exit; while main is blocked in join(), there is still a live non-daemon thread.
7Conclusion
In this answer, we saw that the JVM exits when the last non-daemon thread finishes and does not wait for daemon threads. We create a daemon with setDaemon(true) before start(), since calling it afterwards throws IllegalThreadStateException, and new threads inherit the status of their creator. At exit, daemon threads are abandoned without running finally blocks or flushing buffers, so they suit only work that may be lost. Virtual threads are always daemons, so we must join them or close their executor.
Open with
A daemon thread is a thread the JVM doesn't wait for. The JVM keeps running as long as at least one non-daemon thread is alive, and the main thread is non-daemon.
Then, if they want more
When the last non-daemon thread finishes, the JVM runs its shutdown hooks and halts, even if daemon threads are in the middle of something. To create one, I call setDaemon(true) before start; once the thread is running, setDaemon throws IllegalThreadStateException. With the Java 21 builder I can write Thread.ofPlatform().daemon(true).
A new thread inherits the daemon status of the thread that creates it, so threads made from main are non-daemon by default, and that's also why the pool's thread factory matters: the default executor factory makes non-daemon threads, while the ForkJoinPool common pool uses daemon threads. When the JVM exits, daemon threads are simply abandoned.
Their stacks aren't unwound, finally blocks and try-with-resources don't run, and buffered output can be lost. So I only use daemon threads for work that's safe to drop, like heartbeats or cache refresh, and never for writing data.
Finally, virtual threads are always daemon threads, and setDaemon(false) on them throws IllegalArgumentException, so a command-line program must join them or close their executor before main returns.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 21
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- java.util.concurrent, package documentation, JLS chapter 17, Threads and Locks
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "What is a daemon thread, how do you create one, and what happens to it when the JVM exits?": https://scrivio-interview-library.netlify.app/java/threads/d4c74576a9/