Why must setDaemon(true) be called before start(), and what does a freshly created thread's daemon status default to?
answer
- Flag feeds JVM shutdown bookkeeping
- Javadoc: call it before start()
- Live thread -> IllegalThreadStateException
- Default = inherit creator's daemon flag
- Daemon thread spawns daemon children
basics
~20 sOnce a thread is running, the JVM has counted it as user or daemon, so setDaemon on a live thread throws IllegalThreadStateException. A new platform thread inherits its creator's daemon status; a virtual thread is always a daemon.
solid answer
~50 sThe daemon flag feeds the JVM's shutdown bookkeeping: whether this thread counts as one that keeps the process alive. The Javadoc requires setDaemon before start(), and calling it on a live thread throws IllegalThreadStateException (on a terminated one the behaviour is unspecified). The reason is that bookkeeping: HotSpot counts a thread as user or daemon when it starts and reads the flag again when it exits, so a flip in between would corrupt the count its shutdown check relies on. As for the default, a platform thread inherits the daemon status of the thread that creates it. The main thread is a user thread, so threads created from main are user threads unless you call setDaemon(true), while a thread spawned by a daemon is a daemon. Executors.defaultThreadFactory overrides that and makes every thread it creates non-daemon, and a virtual thread is always a daemon.
go deeper
Knows setDaemon must be called before start() and that calling it on a running thread throws an exception.
Explains why the flag must be set before start() (shutdown bookkeeping) and that the default is inherited from the creating thread, not fixed.
Connects the inheritance rule to ThreadFactory/pool design, ensuring pool threads have intentional daemon status, and names IllegalThreadStateException precisely.
Reasons about liveness/shutdown invariants the flag protects and standardizes daemon policy across an application's thread factories to avoid accidental process-exit data loss.
## Setup: thread states A Java `Thread` moves through a lifecycle. Right after `new Thread(...)` it is in the **NEW** state — created but not yet executing. Calling **`start()`** transitions it to **RUNNABLE** and actually begins running its `run()` method on a separate execution path. Once started, a thread can never go back to NEW and can never be started again. ## What the daemon flag controls The **daemon flag** is a boolean on each thread that tells the JVM whether this thread is a *background* (daemon) thread or a *foreground* (user) thread. The JVM keeps a running notion of how many **non-daemon (user) threads** are alive; when that count drops to zero for the first time, the JVM starts its shutdown sequence. So the daemon flag is an input to the JVM's **shutdown bookkeeping**. ## Why it must be set before start() Because the flag participates in liveness counting, it must be decided **before** the thread begins to run. If you could flip the flag on a thread that is already executing, you could change — mid-flight — whether it counts toward keeping the JVM alive. That would corrupt the JVM's shutdown check ("are there any user threads left?"): HotSpot adds a starting thread to its count of non-daemon threads when its flag says user, and reads the flag again when the thread exits to take it off that count. The Javadoc itself simply says the method must be invoked before the thread is started. Concretely, `setDaemon(boolean)` checks whether the thread is alive. If it is (started and not yet terminated), it throws **`IllegalThreadStateException`** — an unchecked exception signaling the thread is in the wrong state for this operation. On a thread that has already terminated, the Javadoc leaves the behaviour unspecified, and JDK 25 and 27 simply set the flag. ```java Thread t = new Thread(task); t.start(); t.setDaemon(true); // throws IllegalThreadStateException ``` ## The default: inheritance, not a fixed value A common misconception is "new threads are always user threads." The truth: a new thread **inherits the daemon status of the thread that constructs it**. The mechanism: the `Thread` constructor reads the creating thread's daemon flag and copies it into the new thread. - The **main** thread is created by the JVM as a **user** thread. - Therefore threads you create *from* main are user threads by default. - But a thread created *by a daemon thread* is a **daemon** by default. - A **virtual thread** (final since JDK 21) does not inherit: it is always a daemon, and at JDK 25 and 27 `setDaemon(false)` on one throws `IllegalArgumentException`. This is easy to forget inside thread pools or background frameworks: if a daemon worker thread spawns helper threads without calling `setDaemon(false)`, those helpers are daemons too, and may be abandoned at JVM exit. `Executors.defaultThreadFactory()` is the standard exception: it resets every thread it creates to non-daemon, whoever calls it. ## Practical takeaways 1. Set the flag immediately after construction, before `start()`. 2. Don't assume a default — be explicit when correctness depends on it. 3. In `ThreadFactory` implementations (used by `ExecutorService`), set `setDaemon(...)` deliberately so pool threads have a known, intentional liveness behavior.
- A daemon background worker creates a new Thread without calling setDaemon. Is the new thread a daemon?Yes. The new thread inherits the daemon status of its creator, and the creator is a daemon, so the child is a daemon unless explicitly set to false.
- Claiming new threads are unconditionally user threads (they inherit)
- Thinking setDaemon throws a checked exception
- Believing you can toggle daemon status on a live thread
- Forgetting that a thread a daemon creates with new Thread(...) is a daemon too
Assumes JDK 27, the same in JDK 25: the inheritance and setDaemon rules are for platform threads; virtual threads, final since JDK 21, are always daemons.
context
- Thread (Java SE 27 & JDK 27)docs.oracle.com
- Executors (Java SE 27 & JDK 27)docs.oracle.com