Multithreading
Thread Lifecycle
Understand the different states of a Java thread and transitions between them.
Interview: Deeply tests understanding of the JVM thread state diagram (NEW, RUNNABLE, WAITING, TIMED_WAITING, BLOCKED, TERMINATED).
In the JVM, a thread has a defined lifecycle, transitioning through various states represented by the Thread.State enum. Recognizing what causes a thread to move from one state to another is vital for debugging resource bottlenecks and locks.
Core Idea
A thread transitions from NEW to RUNNABLE when started, and goes to WAITING/BLOCKED/TIMED_WAITING when it awaits resources or locks.
Why It Matters
Analyzing thread dumps in production depends heavily on identifying which threads are in BLOCKED or WAITING states.
Interview Lens
Expect questions on what actions trigger specific state transitions (like wait() vs sleep()).
The Six Thread States
The Thread.State enum defines the following six states:
- NEW: A thread object has been instantiated but
start()has not yet been called. - RUNNABLE: The thread is actively executing in the JVM or is ready and waiting for CPU allocation from the OS scheduler.
- BLOCKED: The thread is waiting to acquire an monitor/intrinsic lock to enter a synchronized method or block.
- WAITING: The thread is waiting indefinitely for another thread to perform a specific action (e.g. calling
object.wait()orthread.join()). - TIMED_WAITING: The thread is waiting for another thread for up to a specified waiting time (e.g. calling
Thread.sleep(ms),object.wait(ms), orthread.join(ms)). - TERMINATED: The thread has completed its execution (its
run()method has finished).
Code Walkthrough
This program demonstrates how to query and print a thread's state at various points in its lifecycle.
public class ThreadLifecycleDemo { public static void main(String[] args) throws InterruptedException { Thread thread = new Thread(() -> { try { // TIMED_WAITING state demonstration Thread.sleep(500); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } });System.out.println("State after creation: " + thread.getState()); // NEW
thread.start(); System.out.println("State after start(): " + thread.getState()); // RUNNABLE
Thread.sleep(100); System.out.println("State during sleep(): " + thread.getState()); // TIMED_WAITING
thread.join(); // Wait for completion System.out.println("State after execution completes: " + thread.getState()); // TERMINATED } }
Interview-Relevant Information
Q: What is the difference between BLOCKED and WAITING states?
Answer: A thread is in the BLOCKED state when it is waiting to acquire an intrinsic monitor lock to enter/re-enter a synchronized block or method. A thread is in the WAITING state when it has voluntarily released its lock and is waiting for another thread to send a notification signal (like notify()) or finish execution.
Quick Checklist
Can you name all six thread states? What is the difference between WAITING and TIMED_WAITING? If yes, you understand the thread lifecycle.
Use Cases
Monitoring thread states via JConsole or JVisualVM to detect deadlocks.
Analyzing thread dumps to find performance bottlenecks caused by excessive BLOCKED threads.
Common Mistakes
Assuming a sleeping thread is in the WAITING state (it is actually in the TIMED_WAITING state).
Thinking a thread returns to the NEW state after it completes execution (it goes to TERMINATED).