What is the Thread Lifecycle in Java?
Understand the Java thread lifecycle and all six Thread.State values: NEW, RUNNABLE, BLOCKED, WAITING, TIMED_WAITING and TERMINATED, with code examples.
Expected Interview Answer
The thread lifecycle in Java is the set of states a thread moves through from creation to termination: NEW, RUNNABLE, BLOCKED, WAITING, TIMED_WAITING, and TERMINATED, as defined by the Thread.State enum.
A thread starts as NEW when created but before start() is called. After start() it becomes RUNNABLE, meaning eligible to run while the scheduler decides when it actually executes on a CPU. It moves to BLOCKED while waiting for a monitor lock, to WAITING or TIMED_WAITING when it calls wait(), join(), or sleep(), and finally to TERMINATED once run() completes or an uncaught exception ends it. Transitions are driven by method calls, lock availability, and the scheduler.
- Explains why a started thread may not run immediately
- Helps diagnose deadlocks and stuck threads via thread states
- Clarifies how sleep, wait, and join affect execution
- Guides correct use of notify/notifyAll to wake waiting threads
- Makes thread dumps and profilers easier to interpret
AI Mentor Explanation
A batter in the pavilion is NEW: padded up but not on the field. Walking out to bat is RUNNABLE, ready and eligible to face deliveries. Waiting for a review decision from the third umpire is WAITING, and a rain break with a set restart time is TIMED_WAITING. Being dismissed and returning to the pavilion for good is TERMINATED, out of the innings.
Step-by-Step Explanation
Step 1
NEW
The Thread object is created but start() has not been called yet, so no OS thread exists.
Step 2
RUNNABLE
After start(), the thread is eligible to run; the scheduler decides when it gets CPU time.
Step 3
BLOCKED
The thread is waiting to acquire a monitor lock held by another thread to enter a synchronized region.
Step 4
WAITING
The thread waits indefinitely after wait(), join() with no timeout, or LockSupport.park() until signalled.
Step 5
TIMED_WAITING
The thread waits for a bounded time via sleep(ms), wait(ms), or join(ms) and resumes when the time elapses.
Step 6
TERMINATED
run() has completed or an uncaught exception ended the thread; it cannot be started again.
What Interviewer Expects
- Names all six Thread.State values correctly
- Explains NEW to RUNNABLE happens via start(), not the constructor
- Distinguishes BLOCKED (lock) from WAITING (signal)
- Knows what triggers TIMED_WAITING (sleep, timed wait/join)
- Understands a TERMINATED thread cannot be restarted
Common Mistakes
- Claiming there is a separate RUNNING state (Java folds it into RUNNABLE)
- Confusing BLOCKED with WAITING
- Thinking start() runs the thread immediately
- Believing a terminated thread can be started again
- Assuming sleep() releases held locks (it does not)
Best Answer (HR Friendly)
“A Java thread moves through several stages during its life: created, ready to run, actually running, temporarily paused or waiting, and finally finished. Knowing these stages helps developers understand why threads sometimes wait and how to fix programs that get stuck.”
Code Example
public class ThreadLifecycleDemo {
public static void main(String[] args) throws InterruptedException {
Thread worker = new Thread(() -> {
try {
Thread.sleep(200); // -> TIMED_WAITING
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
});
System.out.println("After creation: " + worker.getState()); // NEW
worker.start();
System.out.println("After start: " + worker.getState()); // RUNNABLE
Thread.sleep(50);
System.out.println("During sleep: " + worker.getState()); // TIMED_WAITING
worker.join(); // main waits for worker to finish
System.out.println("After finish: " + worker.getState()); // TERMINATED
}
}Follow-up Questions
- What is the difference between BLOCKED and WAITING states?
- Does sleep() release the locks a thread holds?
- How does wait() differ from sleep() in terms of state and locks?
- Why can't a TERMINATED thread be started again?
- How do notify() and notifyAll() move a thread out of WAITING?
MCQ Practice
1. Which state is a thread in immediately after being created but before start() is called?
A freshly constructed Thread object is in the NEW state until start() is invoked.
2. A thread waiting to acquire a monitor lock to enter a synchronized block is in which state?
Waiting for a monitor lock puts a thread in the BLOCKED state; WAITING is for signal-based waiting like wait()/join().
3. Which call moves a thread into TIMED_WAITING?
sleep(ms), wait(ms), and join(ms) all cause TIMED_WAITING. Argument-less wait()/join() cause plain WAITING.
Flash Cards
How many thread states does Thread.State define? — Six: NEW, RUNNABLE, BLOCKED, WAITING, TIMED_WAITING, TERMINATED.
What moves a thread from NEW to RUNNABLE? — Calling start(), which asks the JVM to schedule the thread; the constructor alone does not.
BLOCKED vs WAITING? — BLOCKED waits for a monitor lock; WAITING waits indefinitely for a signal such as notify() or a join to finish.
Can a TERMINATED thread run again? — No. Once run() completes, the thread is dead; calling start() again throws IllegalThreadStateException.