Category: java
The Six Java Thread States
Published on 27 Oct 2026
Explanation
Java defines NEW, RUNNABLE, BLOCKED, WAITING, TIMED_WAITING, and TERMINATED. Thread.getState() reports a snapshot of a thread's state; it should not be used as a synchronization mechanism.
Code:
public class ThreadStatesDemo {
public static void main(String[] args) throws InterruptedException {
Thread t = new Thread(() -> System.out.println("Running"));
System.out.println(t.getState()); // NEW
t.start();
t.join();
System.out.println(t.getState()); // TERMINATED
}
}
Explanation
A thread is NEW after its Thread object is created but before start() is called. After start(), it becomes eligible to run and is represented by RUNNABLE, which includes both ready-to-run and currently executing states.
Code:
public class NewRunnableDemo {
public static void main(String[] args) {
Thread t = new Thread(() -> System.out.println("Task running"));
System.out.println("Before start: " + t.getState());
t.start();
}
}
Explanation
A thread is BLOCKED when it is waiting to acquire a Java monitor lock held by another thread. Once the lock becomes available, it can acquire the monitor and continue.
Code:
public class BlockedStateDemo {
private static final Object LOCK = new Object();
public static void main(String[] args) {
Thread first = new Thread(() -> {
synchronized (LOCK) {
try { Thread.sleep(500); }
catch (InterruptedException e) { Thread.currentThread().interrupt(); }
}
});
Thread second = new Thread(() -> {
synchronized (LOCK) { System.out.println("Second acquired lock"); }
});
first.start();
second.start();
}
}
Explanation
WAITING means a thread is waiting indefinitely for another action, for example join() without a timeout. TIMED_WAITING means it waits for a limited time, such as during Thread.sleep() or a timed join().
Code:
public class TimedWaitingDemo {
public static void main(String[] args) throws InterruptedException {
Thread t = new Thread(() -> {
try { Thread.sleep(1000); }
catch (InterruptedException e) { Thread.currentThread().interrupt(); }
});
t.start();
Thread.sleep(100);
System.out.println(t.getState());
t.join();
}
}
Explanation
A thread becomes TERMINATED when its run() method finishes. A Thread object cannot be restarted; calling start() a second time throws IllegalThreadStateException. Create a new Thread object to run a task again.
Code:
public class TerminatedDemo {
public static void main(String[] args) throws InterruptedException {
Thread t = new Thread(() -> System.out.println("Completed"));
t.start();
t.join();
System.out.println(t.getState()); // TERMINATED
}
}