Java Thread Lifecycle
In this page:
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
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
}
}
Login to try C/C++/Java/PHP code in the editor
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
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();
}
}
Login to try C/C++/Java/PHP code in the editor
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
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();
}
}
Login to try C/C++/Java/PHP code in the editor
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
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();
}
}
Login to try C/C++/Java/PHP code in the editor
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
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 try C/C++/Java/PHP code in the editor
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: