EasyExperience level: 0-2 years · 3-5 years4 min read

What Is a Daemon Thread, and Why Does the JVM Exit With Them Still Running?

Reviewed by Gurusankar M. · Updated Aug 24, 2026

Asked inInfosysTCSCognizant
#daemon thread#setdaemon#jvm exit#background threads
Report issue

Short Answer

The JVM keeps running as long as any non-daemon (user) thread is alive, and exits the instant the last one finishes — regardless of whether daemon threads are still running. Daemon threads are simply terminated at that point, mid-instruction, with no finally blocks guaranteed to run. Set via thread.setDaemon(true), called before start(). They're meant for background housekeeping (cache eviction, metrics collection) that shouldn't itself keep the process alive — never put work that must complete, like flushing a buffer or releasing an external lock, on a daemon thread.

On this page

Concept Made Simple

Coffee Chat Question

“What is a daemon thread, and why does the JVM exit even while daemon threads are still running?”

Remember It Faster

Mind Map Answer

Every thread is either a user thread or a daemon thread. The JVM stays alive as long as at least one user thread is running; the moment the last user thread finishes, the JVM exits immediately — and any daemon threads still executing are simply terminated, mid-instruction, with no finally block guaranteed to run and no cleanup performed.

User thread
JVM waits for these before exiting
Daemon thread
does NOT keep the JVM alive; killed abruptly on exit
Set via
thread.setDaemon(true) — must be called BEFORE start()
Typical use
background housekeeping, not work that must complete
java
Thread housekeeper = new Thread(() -> evictExpiredEntries());
housekeeper.setDaemon(true);   // must be set before start()
housekeeper.start();

// If this is the last other thread running, main() returning ends
// the JVM immediately - housekeeper is killed mid-eviction, no warning.

Key takeaway: the classic bug is making a thread daemon so it 'doesn't block shutdown,' then quietly relying on it to finish something that matters — flushing a write buffer, releasing an external lock, closing a file handle. Because daemon threads offer zero cleanup guarantee, that work may simply never happen. If completion matters, either use a user thread and shut it down explicitly (signal it, then join with a timeout), or accept that the work is genuinely disposable.

The garbage collector's own threads are daemon threads, and that's the cleanest example of the rule: GC exists to serve the running application, so it should never be the reason the JVM refuses to exit. The moment the last real (user) thread finishes, the GC thread is torn down along with everything else — nobody wants a process that can't shut down because garbage collection is 'still busy.' Contrast that with ExecutorService worker threads, which default to non-daemon unless you supply a custom ThreadFactory — that's exactly why a forgotten executor.shutdown() is one of the most common reasons a Java process hangs on exit instead of terminating: those threads are user threads by default, so the JVM waits for them.

Think Beyond the Expected

What If?

A background metrics-uploader is a daemon thread, and on shutdown the last few seconds of metrics are sometimes missing — why?

Because it's a daemon thread, the JVM doesn't wait for it — if the last user thread finishes first, the uploader is killed mid-upload with no chance to flush. Add a shutdown hook that explicitly signals the uploader to flush and join()s it with a timeout, or make it a non-daemon thread with a proper, explicit stop mechanism instead of relying on daemon semantics.

Real World

Scheduled background workers — cache evictors, metrics collectors, connection-pool reapers — are usually daemon threads by design, precisely because they shouldn't hold the process open on their own. The bug shows up when someone assumes 'daemon' just means 'runs quietly in the background' and forgets it also means 'can be killed at literally any instant with zero warning.'

Real Talk from Guru

When I see setDaemon(true) in a review, my first question is always 'what happens if this thread is killed mid-line, right now?' If the honest answer involves lost data or an unreleased resource, it shouldn't be a daemon thread — or it needs an explicit shutdown-hook-driven stop, not an assumption that it'll get to finish.

Interviewer's Expectation

Keywords they're listening for:

  • JVM exits once the last non-daemon thread finishes
  • daemon threads are killed abruptly with no cleanup guaranteed
  • setDaemon(true) must be called before start()
  • never rely on a daemon thread to complete must-finish work

Common Mistakes

  • Calling setDaemon(true) after start() (throws IllegalThreadStateException)
  • Relying on a daemon thread to complete cleanup or flush work during shutdown
  • Assuming ExecutorService threads are daemon by default — they're user threads unless you supply a custom ThreadFactory

Best Practices

  • Use daemon threads only for genuinely disposable background work
  • For work that must complete, use a shutdown hook to explicitly signal and join with a timeout
  • Set daemon status before start(), never after

Follow-up Questions

  1. What happens if you call setDaemon(true) after the thread has already started?
  2. Are the worker threads in a default ExecutorService pool daemon or user threads?
  3. How do you cleanly stop a background daemon thread instead of relying on it being killed?
  4. Why is the garbage collector's own thread a daemon thread?

Continue Learning with AI

Take this question deeper with your favourite AI assistant. Pick a depth, copy the prompt, or open it directly — AI is your learning companion, not a shortcut.

Plain-language foundations

I'm preparing for a software engineering interview and want to understand this from scratch, as a beginner.

Topic: Fundamentals (Multithreading)
Interview question: "What is a daemon thread, and why does the JVM exit even while daemon threads are still running?"

Please:
1. Explain the core idea in simple, plain language, using an everyday analogy.
2. Define any technical terms you use.
3. Walk through one small, concrete example.
4. Finish with a single sentence I can easily remember.

Keep the tone friendly and assume I'm new to this topic.
Open inChatGPTGeminiClaude

Was this answer helpful?

Affiliate links

Equipment for a comfortable, productive setup.

As an Amazon Associate I earn from qualifying purchases.