Thread Life Cycle in Java
Every thread in Java goes through a life cycle: it is created, it runs, it may wait, and it finally ends. At any moment, a thread is in exactly one of six states, defined in the enum Thread.State: NEW, RUNNABLE, BLOCKED, WAITING, TIMED_WAITING and TERMINATED. You can check a thread's state with getState(). In this lesson, you will learn what each state means and which actions move a thread from one state to another.
The Six Thread States
| State | Meaning | How a thread gets there |
|---|---|---|
NEW | Created but not started | new Thread(...) |
RUNNABLE | Running, or ready to run when the CPU is free | start() |
BLOCKED | Waiting to enter a synchronized block held by another thread | Trying to take a busy lock |
WAITING | Waiting with no time limit for another thread | join(), wait(), LockSupport.park() |
TIMED_WAITING | Waiting for a limited time | sleep(ms), join(ms), wait(ms) |
TERMINATED | Finished | run() returned or threw an exception |
Example: Watching a Thread Change State
Let's see an example that prints a thread's state at each stage.
public class States {
public static void main(String[] args) throws InterruptedException {
Object lock = new Object();
Thread t = new Thread(() -> {
try {
Thread.sleep(300);
} catch (InterruptedException e) {
return;
}
synchronized (lock) {
System.out.println("worker got the lock");
}
});
System.out.println("After new: " + t.getState());
t.start();
Thread.sleep(100);
System.out.println("While sleep: " + t.getState());
synchronized (lock) {
Thread.sleep(400);
System.out.println("Lock busy: " + t.getState());
}
t.join();
System.out.println("After end: " + t.getState());
}
}After new: NEW
While sleep: TIMED_WAITING
Lock busy: BLOCKED
worker got the lock
After end: TERMINATEDExplanation: Before start(), the thread is NEW. While it sleeps, it is TIMED_WAITING. The main thread then holds the lock; when the worker wakes up and tries to enter the synchronized block, it is BLOCKED. Once main releases the lock, the worker runs, finishes, and becomes TERMINATED.
Common Mistakes
Calling start() on a TERMINATED thread. It throws IllegalThreadStateException; threads can't be restarted.
Looking for a "RUNNING" state. It's part of RUNNABLE.
Confusing BLOCKED with WAITING. BLOCKED waits for a monitor lock; WAITING waits for another thread's action.
Interview Corner
What are the states of a thread in Java?
Thread.State defines six states. NEW: created but not started. RUNNABLE: executing or ready to execute. BLOCKED: waiting to acquire a monitor lock to enter a synchronized block or method. WAITING: waiting indefinitely for another thread, for example in join() or wait(). TIMED_WAITING: waiting for a bounded time, for example in sleep(ms) or join(ms). TERMINATED: run() has completed or thrown.
Practice
- Easy: Print the state of a thread before starting it, right after starting it, and after joining it.
- Harder: Make a thread call
join()on another thread that sleeps for one second, and print the first thread's state while it waits. Which state is it, and why?
Key Takeaways
Was this tutorial helpful?