A Java thread passes through several states from creation to completion. The JVM manages these states throughout the thread's execution. Understanding the thread lifecycle is important for developing efficient and reliable multithreaded applications.
Key Points: • A thread moves through different states during its lifetime. • The JVM controls transitions between thread states. • A thread can be blocked or waiting before it resumes execution. • Once a thread completes execution, it enters the terminated state. • Proper understanding of thread states helps in debugging concurrency issues.
Thread Lifecycle States
1. New
A thread is in the New state when it has been created but has not yet started.
Example:
Thread thread = new Thread();
State:
NEW
Characteristics:
• Thread object exists. • JVM has not scheduled it for execution. • start() has not been called.
2. Runnable
After calling start(), the thread enters the Runnable state.
Example:
thread.start();
State:
RUNNABLE
Characteristics:
• Thread is ready to execute. • Waiting for CPU allocation. • JVM scheduler decides when it runs.
Note:
In Java, Runnable includes both ready-to-run and running states.
3. Running
The thread enters the Running state when the CPU starts executing its run() method.
Characteristics:
• Thread instructions are actively executing. • Only one thread can run on a CPU core at a given moment.
Example:
public void run() {
System.out.println(
"Thread is running");
}4. Blocked
A thread enters the Blocked state when it is waiting to acquire a monitor lock.
Example:
Two threads trying to enter the same synchronized block.
Characteristics:
• Cannot proceed until the lock becomes available. • Automatically moves back to Runnable after acquiring the lock.
5. Waiting
A thread enters the Waiting state when it waits indefinitely for another thread to perform a specific action.
Methods Causing Waiting:
• wait() • join() • LockSupport.park()
Characteristics:
• Thread remains inactive until notified. • Consumes minimal CPU resources.
6. Timed Waiting
A thread enters the Timed Waiting state when it waits for a specified duration.
Methods Causing Timed Waiting:
• Thread.sleep() • wait(timeout) • join(timeout)
Example:
Thread.sleep(5000);
Characteristics:
• Waits for a fixed period. • Automatically returns to Runnable after timeout.
7. Terminated (Dead)
A thread enters the Terminated state after completing execution or if an uncaught exception ends the thread.
Characteristics:
• Execution is finished. • Cannot be restarted. • Calling start() again throws an exception.
Example:
run() method completes successfully.
Thread Lifecycle Flow
NEW | start() | RUNNABLE | CPU Assigned | RUNNING | ----------------------------- | | | | | | BLOCKED WAITING TIMED_WAITING | | | ----------------------------- | RUNNABLE | Execution Complete | TERMINATED
Example: Consider an online banking application.
Thread 1:
• Process payment
Thread 2:
• Generate transaction report
Thread 3:
• Send notification
Each thread may move between Runnable, Waiting, and Running states based on resource availability and execution flow.
Code Example:
public class Demo {
public static void main(String[] args) {
Thread thread = new Thread(() -> {
System.out.println(
"Thread Running");
});
System.out.println(
thread.getState());
thread.start();
System.out.println(
thread.getState());
}
}Possible Output:
NEW
RUNNABLEReal-World Example
Suppose a user uploads a file:
• New → Thread created • Runnable → Ready for execution • Running → Processing file • Waiting → Waiting for database response • Runnable → Response received • Running → Continue processing • Terminated → Task completed
Interview Tip: A concise interview answer is:
"A Java thread lifecycle consists of New, Runnable, Blocked, Waiting, Timed Waiting, and Terminated states. A thread starts in the New state, moves to Runnable when start() is called, may enter Blocked or Waiting states during execution, and finally reaches the Terminated state after completing its run() method."