← Back to Advanced Java Course | Chapter 2: Multithreading | Lesson 2 of 8

Java Thread Lifecycle

Thread States Overview

A thread passes through several distinct states during its lifecycle: NEW, RUNNABLE, BLOCKED, WAITING, TIMED_WAITING, and TERMINATED. The JVM automatically manages these state transitions as a thread is created, scheduled, paused, and eventually finishes running.

Example: Thread States Overview

java
public class Main {
	public static void main(String[] args) throws InterruptedException {
		Thread t = new Thread(() -> {});
		System.out.println(t.getState()); // NEW
		t.start();
		t.join();
		System.out.println(t.getState()); // TERMINATED
	}
}

New and Runnable States

A thread sits in the NEW state the moment it's created but before start() has been called on it. Calling start() immediately transitions it into the RUNNABLE state, where it becomes eligible for CPU scheduling but isn't necessarily running yet.

Example: New and Runnable States

java
public class Main {
	public static void main(String[] args) throws InterruptedException {
		Thread t = new Thread(() -> System.out.println("running"));
		System.out.println(t.getState()); // NEW
		t.start();
		System.out.println(t.getState()); // RUNNABLE
		t.join();
	}
}

Timded Waiting State

A thread enters the TIMED_WAITING state when it pauses itself for an explicit, bounded duration, most commonly by calling the static Thread.sleep(milliseconds) method. Unlike an indefinite WAITING state, a timed-waiting thread will resume automatically once that duration elapses, even without any outside signal.

Example: Timded Waiting State

java
public class Main {
	public static void main(String[] args) throws InterruptedException {
		Thread t = new Thread(() -> {
			try { Thread.sleep(200); } catch (InterruptedException e) {}
		});
		t.start();
		Thread.sleep(50);
		System.out.println(t.getState()); // TIMED_WAITING
		t.join();
	}
}

Blocked and Waiting States

A thread enters the BLOCKED state specifically while it's waiting to acquire a lock on a resource that another thread currently holds. It enters the more general WAITING state when it's paused indefinitely for another thread to perform some action, such as calling notify() or completing a join().

Example: Blocked and Waiting States

java
public class Main {
	static final Object lock = new Object();
	public static void main(String[] args) throws InterruptedException {
		Thread holder = new Thread(() -> {
			synchronized (lock) {
				try { Thread.sleep(200); } catch (InterruptedException e) {}
			}
		});
		holder.start();
		Thread.sleep(50);
		Thread blocked = new Thread(() -> {
			synchronized (lock) { }
		});
		blocked.start();
		Thread.sleep(50);
		System.out.println(blocked.getState()); // BLOCKED
		holder.join();
		blocked.join();
	}
}

Terminated State

A thread enters the TERMINATED state once its run() method finishes executing, either normally or by throwing an uncaught exception. A terminated thread is done for good and cannot be restarted; you'd need to create a brand-new Thread object to run that code again.

Example: Terminated State

java
public class Main {
	public static void main(String[] args) throws InterruptedException {
		Thread t = new Thread(() -> System.out.println("done"));
		t.start();
		t.join();
		System.out.println(t.getState()); // TERMINATED
	}
}

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.