What is the difference between a java.lang.Thread object and the OS thread behind it, before start(), at start() and after termination?
answer
- two lifetimes, one name
- start0 crosses into the VM
- eetop non-zero means alive
- exit: clear, TERMINATED, notify
- at 27, a dead Thread keeps its Runnable
basics
~20 sA platform Thread is a plain heap object; the OS thread is what the kernel schedules. On HotSpot, new Thread creates only the object, start() creates the OS thread, and termination ends it while the object lives on if still referenced.
solid answer
~50 s`new Thread(task)` only builds a Java object holding the task, name and priority: `getState()` says `NEW` and no kernel thread exists. On JDK 27's HotSpot, `start()` calls the private native `start0()`, which creates a native `JavaThread` plus the OS thread and its stack, links them through the private `eetop` field and runs `run()` on the new thread; if the OS refuses, `start()` throws `OutOfMemoryError`. While a platform thread lives its object stays reachable, even if your code drops every reference. At the end the VM clears the thread-locals, sets `TERMINATED`, zeroes `eetop`, wakes `join()` callers and frees the native side. The object then remains an ordinary collectable object: `getName()` still answers and, on JDK 25 and 27, its `Runnable` stays reachable through it, but `start()` throws `IllegalThreadStateException`. A virtual thread is a `Thread` object that typically has no OS thread of its own.
code
java · 18 linespublic class ThreadObjectDemo {
public static void main(String[] args) throws InterruptedException {
Runnable task = () -> System.out.println("task on " + Thread.currentThread().getName());
Thread t = new Thread(task, "worker-1");
System.out.println(t.getState() + " alive=" + t.isAlive());
t.start();
t.join();
System.out.println(t.getState() + " alive=" + t.isAlive() + " name=" + t.getName());
t.run();
try {
t.start();
} catch (IllegalThreadStateException e) {
System.out.println("start() again: " + e.getClass().getSimpleName());
}
}
}go deeper
Recall that new Thread makes only a Java object, start() makes the real thread, and a finished Thread object can still be inspected but never started again.
Explain the start path: private native start0, the VM's thread structure, the OS thread, eetop set, run invoked. Then the exit path: thread-locals cleared, TERMINATED set, join callers notified, native side freed.
Show the production reading: a native-thread OutOfMemoryError comes from start, not new Thread. Live thread counts track started threads, while retained dead Thread objects cost only heap, including, on JDK 25 and 27, the Runnable they still hold.
Frame the tradeoff: each platform thread is a kernel resource with a stack, which is why teams cap them with pools or move blocking work to virtual threads, whose Thread objects are typically not tied to one OS thread.
## Two things that share a name A **`java.lang.Thread` object** is an ordinary Java object on the heap. An **OS thread** (or kernel thread) is what the operating system schedules onto a processor, with its own stack. The Java Language Specification (JLS) ties them loosely: "Threads are represented by the Thread class", and a thread starts when `start()` is invoked on the corresponding object. The `Thread` Javadoc adds that **platform threads** are "typically mapped 1:1 to kernel threads", so the mapping is described as typical, not promised. The mechanics below are HotSpot's, read from JDK 27 and the same in JDK 25's source. ## Before start(): only an object `new Thread(task)` runs a constructor that fills in Java fields. - The task, requested stack size, priority and daemon flag go into a private nested `FieldHolder`. - The identifier returned by `threadId()` and the name are assigned now. - Inheritable thread-local values are copied now, on the creating thread. - The private `eetop` field is 0, `getState()` returns `NEW`, `isAlive()` is false and `getStackTrace()` returns an empty array. On both releases, creating 1,000 unstarted threads leaves `ThreadMXBean.getThreadCount()` unchanged, and a dropped unstarted `Thread` is collected like any other object. The Javadoc also says unstarted non-daemon threads do not prevent the shutdown sequence from beginning. ## What start() allocates 1. `start()` locks the `Thread` object and throws `IllegalThreadStateException` if the private status field is not zero. 2. It calls the private native `start0()`, which is bound to HotSpot's `JVM_StartThread`. 3. HotSpot allocates a `JavaThread`, its native bookkeeping structure, whose constructor calls `os::create_thread` to create the OS thread. 4. If no OS thread could be created, `start()` throws `OutOfMemoryError` with the message "unable to create native thread: possibly out of memory or process/resource limits reached". 5. Otherwise `JavaThread::prepare` links the two and stores the native address in `eetop`, which is what makes `isAlive()` true. 6. The status becomes `RUNNABLE`, `os::start_thread` releases the OS thread, and that thread calls `Thread.run()`. Step 4 is why this error comes from `start()` and not from `new Thread`. ## While it runs A platform `Thread` object cannot be collected while its thread is alive. The JLS counts an object as reachable if it can be accessed "from any live thread", and `Thread.currentThread()` gives the running code this object at any moment. A running platform thread whose every program reference has been dropped survives forced collections; once it ends, its object is collected. `getState()` reports the VM's view, not the kernel's: the `Thread.State` Javadoc says these states do not reflect any operating system thread states. ## After termination A thread terminates once `run()` returns, or once it throws and the uncaught exception handler has finished. Then, in this order: 1. The VM calls the private `Thread.exit()`, whose `clearReferences()` nulls `threadLocals`, `inheritableThreadLocals`, the per-thread exception handler and the NIO blocker. 2. HotSpot's `ensure_join` takes the `Thread` object's monitor, sets `TERMINATED`, zeroes `eetop` and notifies all waiters on the object, which releases callers of `join()`. 3. `JavaThread::post_run` deletes the native structure, and the OS thread ends. All three steps run on the exiting thread itself, so `isAlive()` can read false, and `join()` can return, just before the OS thread has ended. What remains then is the Java object, for as long as your code refers to it. | Part | Before `start()` | Running | After termination | |---|---|---|---| | Java object | yes | yes, kept reachable | yes, if you still refer to it | | OS thread and stack | no | yes | finishing its exit, then gone | | `getState()` | `NEW` | `RUNNABLE` or another live state | `TERMINATED` | | `isAlive()` | false | true | false | | Thread-local maps (private) | inherited values only | created on first use | cleared | | Task `Runnable` (private `holder.task`) | held | held | **still held** on JDK 25 and 27 | Three consequences follow: - `getName()`, `threadId()` and any field of your own `Thread` subclass still answer. - On JDK 25 and 27 `holder.task` is `final`, so a retained dead `Thread` keeps its `Runnable`, and whatever the task captured, reachable, and calling `run()` on it runs the task again on the caller's thread. JDK 17's `Thread.exit()` still nulled its task reference. - `start()` throws `IllegalThreadStateException`: a thread can be started at most once. ## What a virtual thread changes A **virtual thread** is also a `Thread` object, with the same `NEW` and `TERMINATED` states, but the Javadoc describes it as typically a user-mode thread scheduled by the Java runtime, running on a small set of platform **carrier threads**. The one-to-one picture and the reachability rule above apply to platform threads: JEP 444 says virtual thread stacks are not GC roots. ## What to do with this - Read a native-thread `OutOfMemoryError` as the OS refusing a new thread, for lack of native memory or a process limit, not only for too many **started** threads. Check live thread counts, native memory, limits and requested stack sizes, not how many `Thread` objects exist. - Treat a retained dead `Thread` object as a heap matter: once its brief exit is over, it holds no OS thread. - Avoid `wait`, `notify` and `notifyAll` on a `Thread` instance: `join()` uses that monitor, and the Javadoc recommends applications not use them there.
- Why does a loop creating a million unstarted Thread objects not fail with "unable to create native thread"?That error comes from `start()`, when HotSpot cannot create the OS thread. `new Thread` only allocates heap objects, so the limit such a loop meets is the heap, which reports a different `OutOfMemoryError`. Starting the threads is what consumes kernel threads, stacks and process limits.
- What does calling run() on a platform Thread that has already terminated do?It is an ordinary method call on the caller's thread. For a `Thread` built with a `Runnable`, the private `holder.task` is still set, so on JDK 25 and 27 the task runs again, on the caller, with no new OS thread. A subclass's overridden `run()` also simply executes. The Javadoc says a direct `run()` on a virtual thread does nothing.
- How does join() learn that a platform Thread has terminated?`join()` loops on `wait()` on the `Thread` object while `isAlive()` is true, inside `synchronized (this)`. As the thread exits, HotSpot's `ensure_join` takes that same monitor, sets `TERMINATED`, zeroes `eetop` and notifies all waiters. The exiting OS thread does this itself and still has teardown to run, so `join()` can return just before it ends. This shared monitor is why the Javadoc recommends that applications not use `wait` or `notify` on `Thread` instances.
- Is it safe to read a field of a Thread subclass after its thread has terminated?Yes, once you have detected the termination. The object is still there, and JLS §17.4.4 says a thread's final action synchronizes-with any action that detects its end, for example through `join()` or `isAlive()`, so writes made in `run()` are visible. Reading without such a detection, or other ordering, is a data race.
A Thread object is a personnel file and the OS thread is the employee on shift. The file exists before the first day, start() is the hire, and after the employee leaves the file can still be read, but nobody can be rehired from it.
- new Thread() creates the OS thread straight away, and start() only releases it.
- When a thread finishes, the JVM destroys its Thread object automatically.
- Dropping the last reference to a running platform Thread lets the GC collect or stop it.
- A finished Thread can be reused by calling start() on it again.
- Java guarantees every Thread object maps one-to-one to a kernel thread.
- On JDK 25 or 27, a terminated Thread has released everything, including its Runnable.
- java.lang.Thread
- Java 1.0
- Thread.getState and Thread.State
- Java 1.5
Assumes JDK 27 on HotSpot; the start and exit paths named here read the same in JDK 25's source.
context
- Thread (Java SE 27 & JDK 27)docs.oracle.com
- Chapter 17. Threads and Locksdocs.oracle.com
- Thread.java at jdk-27-gagithub.com