What are the thread states in Java, and how does a thread move between them?
Thread.State defines six states, and the lifecycle is worth being able to recite.
NEW is a thread that has been created but not yet started. RUNNABLE means the thread is running or is queued waiting for a CPU — the JVM lumps those two together, which surprises people who expect a separate running state. BLOCKED means the thread is waiting to enter a synchronized block or method because another thread holds the monitor. WAITING means the thread has called Object.wait, Thread.join or Thread.sleep with no timeout, and it is waiting to be notified. TIMED_WAITING is the same but with a deadline — a sleep with a timeout, or a join with a timeout. TERMINATED means the run method has returned or the thread died from an exception.
The transitions follow from the operations you perform. Calling start() moves NEW to RUNNABLE. A thread that tries to take a monitor someone else holds moves to BLOCKED, and it wakes to RUNNABLE when the monitor is released. A thread that calls wait() moves to WAITING, and the notifier's notify() moves it back to RUNNABLE. A thread that finishes moves to TERMINATED, and that is final.
Two distinctions interviewers like. WAITING and TIMED_WAITING are separate states even though the behaviour is similar, because one can end without anybody doing anything. And BLOCKED is not the same as WAITING — BLOCKED is about monitor contention, WAITING is about a deliberate hand-off, and they are resolved by different events.