What are the states in a thread's lifecycle?
7 minintermediatethread-lifecyclethread-states
Quick Answer
A Java thread moves through NEW (created, not started), RUNNABLE (executing or eligible to run, includes what the OS might call 'ready' or 'running'), BLOCKED (waiting to acquire a monitor lock), WAITING (waiting indefinitely for another thread's signal), TIMED_WAITING (waiting for a bounded time), and TERMINATED (finished execution).
Detailed Answer
Thread.State (queryable via thread.getState()) defines six states:
NEW: theThreadobject has been created butstart()hasn't been called yet.RUNNABLE: the thread is executing, or is eligible to execute and waiting for CPU time from the OS scheduler — Java doesn't distinguish "actually running" from "ready to run" at the language level; both map toRUNNABLE.BLOCKED: the thread is waiting to acquire a monitor lock to enter asynchronizedblock/method that another thread currently holds.WAITING: the thread is waiting indefinitely for another thread to perform a specific action — e.g., it calledObject.wait()(no timeout),Thread.join()(no timeout), orLockSupport.park().TIMED_WAITING: likeWAITING, but bounded by a timeout — e.g.,Thread.sleep(ms),Object.wait(timeout),Thread.join(timeout).TERMINATED: the thread'srun()method has completed (normally or via an uncaught exception); it cannot be restarted.
Thread t = new Thread(() -> { try { Thread.sleep(1000); } catch (InterruptedException e) {} });
t.getState(); // NEW
t.start();
t.getState(); // RUNNABLE (or briefly TIMED_WAITING once sleep() kicks in)
t.join();
t.getState(); // TERMINATED
A common interview follow-up: BLOCKED vs WAITING — BLOCKED specifically means contending for a lock to enter a synchronized region; WAITING means the thread voluntarily gave up execution and is waiting for a signal (notify()/notifyAll()) or another thread to finish, having already been inside (or without needing) a lock.