In Java, a thread goes through five states in its lifecycle from creation to termination. These states are managed by the Java Virtual Machine (JVM) and are defined in the Thread.state enum.
Thread Lifecycle States
- New (Created)
- Runnable
- Blocked
- Waiting (or Timed Waiting)
- Terminated (Dead)
Let's see each state of thread in details,
1. New (Created) State
- When a thread is created it is in the new state but not yet started.
- This happens when a thread object is instantiated using
Thread
or a class that implementsRunnable
.
Example
public class ThreadExample {
public static void main(String[] args) {
MyThread t1 = new MyThread(); // Thread created, but not started
System.out.println("Thread created: " + t1.getState()); // Output: NEW
}
}
class MyThread extends Thread {
public void run() {
System.out.println("Thread is running...");
}
}
Key point:
The thread remains in the NEW
state until start()
is called.
2. Runnable (Ready to Run) State
- When
start()
is called the thread enters the Runnable state. - It is ready to run, but it may or may not run depending on CPU scheduling.
Example
public class ThreadExample {
public static void main(String[] args) {
MyThread t1 = new MyThread();
t1.start(); // Now the thread is in Runnable state
System.out.println("Thread state after start(): " + t1.getState());
}
}
Key Point:
The thread is waiting to be selected by the CPU scheduler for execution.
3. Blocked State
- When a thread tries to access a synchronized block or resource that is locked by another thread, it enters the blocked state.
- The thread waits until the lock is released.
Example
public class BlockedThreadExample {
public static void main(String[] args) {
SharedResource resource = new SharedResource();
Thread t1 = new Thread(() -> resource.display(), "Thread-1");
Thread t2 = new Thread(() -> resource.display(), "Thread-2");
t1.start();
t2.start();
try { Thread.sleep(100); } catch (InterruptedException e) {}
System.out.println("Thread-2 state: " + t2.getState()); // Output: BLOCKED (if t1 holds lock)
}
}
class SharedResource {
synchronized void display() {
System.out.println(Thread.currentThread().getName() + " is executing.");
try { Thread.sleep(1000); } catch (InterruptedException e) {}
}
}
Key Point:
The blocked condition occurs due to thread synchronization.
4. Waiting (or Timed Waiting) State
- When a thread calls
wait()
,join()
, orpark()
, it goes into Waiting and waits indefinitely until another thread notifies it. - When a thread calls
sleep(time)
,join(time)
, orwait(time)
, it goes into Timed Waiting, which means it will wait for a certain period of time.
Example
public class WaitingExample {
public static void main(String[] args) {
MyThread t1 = new MyThread();
t1.start();
try {
t1.join(); // Main thread will wait until t1 finishes
} catch (InterruptedException e) {}
System.out.println("Main thread resumes.");
}
}
class MyThread extends Thread {
public void run() {
try { Thread.sleep(5000); } catch (InterruptedException e) {}
System.out.println("Thread execution completed.");
}
}
Key points:
wait()
→ waits until notify() or notifyAll() is called.join()
→ waits for another thread to complete execution.sleep(time)
→ waits for a certain amount of time and then resumes.
5. Terminated (Dead) State
A thread enters the Terminated state when its execution finishes or it is stopped using
stop()
(obsolete).
Example
public class TerminatedExample {
public static void main(String[] args) {
MyThread t1 = new MyThread();
t1.start();
try { Thread.sleep(100); } catch (InterruptedException e) {}
System.out.println("Thread state after completion: " + t1.getState()); // Output: TERMINATED
}
}
class MyThread extends Thread {
public void run() {
System.out.println("Thread is running...");
}
}
Key Point:
When a thread enters the Terminated state, it cannot be restarted.
Summary
- Java follows a well-defined thread lifecycle controlled by the JVM.
- Understanding these states helps in debugging multi-threaded programs and optimizing thread performance.
- Blocked, Waiting, and Timed Waiting states are important in synchronization and concurrency control.
Also, read: What is the difference between wait and sleep in java
Leave Comment