Thread Lifecycle
The six Java thread states and what moves a thread between them, BLOCKED versus WAITING in a thread dump, daemon threads, and how to stop a thread safely in Java and Python.
Overview
A thread is created, started, runs, may wait for locks or for other threads, and finally ends. Knowing those stages lets you read a thread dump, explain why a program hangs or refuses to exit, and stop threads without corrupting data.
Java names the stages exactly: Thread.State has six values, and getState() returns one of them. The docs stress that these are JVM states, not operating-system states, and that getState() is for monitoring, not for synchronization. Python has no state enum; a thread is either not started, alive (is_alive()), or finished.
The six states
A thread moves through six states, and every move has a cause. RUNNABLE sits in the middle: a thread leaves it to wait and comes back to it to run.
- NEW: created, with start() not called yet, as right after new Thread(r). Calling start() moves it to RUNNABLE.
- RUNNABLE: executing in the JVM, possibly waiting for a CPU or other OS resources. It covers both running and ready to run, and a thread blocked in some native I/O shows here too.
- BLOCKED: waiting for a monitor lock to enter a synchronized block or method, or to re-enter one after Object.wait(). The usual cause is that another thread holds the monitor. Once the monitor is acquired, the thread is RUNNABLE again.
- WAITING: waiting with no timeout for another thread to act, after Object.wait(), Thread.join() or LockSupport.park(). The thread returns to RUNNABLE when it is notified, the joined thread ends, or it is unparked.
- TIMED_WAITING: the same, with a timeout: Thread.sleep(ms), wait(ms), join(ms), parkNanos, parkUntil. The thread returns to RUNNABLE when the time is up or it is woken.
- TERMINATED: finished, because run() returned or threw.
So BLOCKED is only for entering, or re-entering after wait(), a synchronized block. Waiting for a signal, a join or a park is WAITING or TIMED_WAITING.
Interviewers often probe two details: first, a thread waiting on a ReentrantLock shows as WAITING (or TIMED_WAITING), not BLOCKED, because those locks park the thread, while BLOCKED means an intrinsic synchronized monitor specifically. Second, a thread can be started only once: calling start() again throws IllegalThreadStateException in Java and RuntimeError in Python, even after it has finished.
The program below drives one thread through NEW, TIMED_WAITING and TERMINATED, and puts two others into WAITING and BLOCKED.
Interview questions
- What are the six thread states in Java?
- What is the difference between BLOCKED and WAITING?
- What is a daemon thread, and when do you use one?
- How do you stop a thread safely?
Sign up free to read the full topic
More Low-Level Design topics
Start free on Cariara · Coding problems · System design studio