Thread Life Cycle in Java

Naveen AnamaneniUpdated · 4 min read

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

StateMeaningHow a thread gets there
NEWCreated but not startednew Thread(...)
RUNNABLERunning, or ready to run when the CPU is freestart()
BLOCKEDWaiting to enter a synchronized block held by another threadTrying to take a busy lock
WAITINGWaiting with no time limit for another threadjoin(), wait(), LockSupport.park()
TIMED_WAITINGWaiting for a limited timesleep(ms), join(ms), wait(ms)
TERMINATEDFinishedrun() returned or threw an exception
start() makes a thread RUNNABLE; it may wait or block, then terminates

Example: Watching a Thread Change State

Let's see an example that prints a thread's state at each stage.

States.java▶ Try it
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());
    }
}
Output
After new:   NEW
While sleep: TIMED_WAITING
Lock busy:   BLOCKED
worker got the lock
After end:   TERMINATED

Explanation: 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

  1. Easy: Print the state of a thread before starting it, right after starting it, and after joining it.
  2. 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?

Quick check: Thread life cycle

4 questions
1What state is a thread in after new Thread(...)?
2What state is a thread in during Thread.sleep(1000)?
3A thread wants a synchronized lock held by another. Which state?
4Is there a RUNNING state in Thread.State?

Key Takeaways

Was this tutorial helpful?

Lesson 379 of 482