Thread Lifecycle
The Thread Lifecycle in Java describes the various states a thread goes through from creation to termination. Understanding these states is essential for multithreading, debugging, performance tuning, and interviews.
What Is Thread Lifecycle?
The thread lifecycle represents the state transitions of a thread during its execution in the JVM.
Java defines thread states in the Thread.State enum.
Thread States in Java
Java has 6 official thread states:
- NEW
- RUNNABLE
- BLOCKED
- WAITING
- TIMED_WAITING
- TERMINATED
1️⃣ NEW State
- Thread object is created
start()not yet called- Thread is not eligible for execution
Thread t = new Thread(); // NEW
- ✔ Memory allocated
- ❌ CPU not allocated
2️⃣ RUNNABLE State
- Thread is ready to run or running
start()has been called- JVM hands over thread to OS scheduler
t.start(); // RUNNABLE
- ✔ Thread may be running or waiting for CPU
- ✔ Java combines Ready + Running into RUNNABLE
3️⃣ BLOCKED State
- Thread is waiting to acquire a monitor lock
- Occurs with synchronized blocks
synchronized (lock) {
// critical section
}
- ✔ Thread waits until lock is released
- ✔ Does NOT consume CPU
4️⃣ WAITING State
- Thread waits indefinitely for another thread’s action
- No timeout specified
Caused by:
wait()join()park()
obj.wait(); // WAITING
✔ Thread resumes only when notified
5️⃣ TIMED_WAITING State
- Thread waits for a specified amount of time
Caused by:
sleep(time)wait(time)join(time)
Thread.sleep(1000); // TIMED_WAITING
✔ Automatically resumes after timeout
6️⃣ TERMINATED State
- Thread execution completed
run()method exits normally or due to exception
// run() method ends
- ✔ Thread cannot be restarted
- ❌ Calling
start()again → IllegalThreadStateException
Thread Lifecycle Flow (Conceptual)
NEW
↓ start()
RUNNABLE
↓ lock unavailable
BLOCKED
↓ lock acquired
RUNNABLE
↓ wait()
WAITING
↓ notify()
RUNNABLE
↓ sleep(time)
TIMED_WAITING
↓ time expires
RUNNABLE
↓ run() ends
TERMINATED
Important Notes (Interview Traps)
start()→ creates new call stack- Calling
run()directly ❌ does NOT start a new thread sleep()does NOT release lockwait()releases lock- BLOCKED ≠ WAITING
Thread State vs Method Mapping
| Method | Resulting State |
|---|---|
start() |
RUNNABLE |
sleep() |
TIMED_WAITING |
wait() |
WAITING |
join() |
WAITING |
| synchronized lock unavailable | BLOCKED |
run() completes |
TERMINATED |
Interview-Ready Answers
Short Answer
Thread lifecycle represents the various states a thread goes through during execution.
Detailed Answer
In Java, a thread goes through six states: NEW, RUNNABLE, BLOCKED, WAITING, TIMED_WAITING, and TERMINATED. These states represent thread creation, execution readiness, lock waiting, indefinite waiting, timed waiting, and completion. The JVM manages these transitions based on thread behavior and synchronization.
Key Takeaway
Threads move through well-defined states controlled by JVM and OS scheduler. Understanding thread lifecycle is critical for writing correct, efficient, and deadlock-free multithreaded programs.