---
title: "Thread Lifecycle in Java"  
description: "A thread states are managed by the Java Virtual Machine (JVM) and defined in the Thread.State enum."  
author: "Ashutosh Patel"  
published: 2025-03-20  
updated: 2025-03-20  
canonical: https://www.mindstick.com/articles/338823/thread-lifecycle-in-java  
category: "java"  
tags: ["java", "thread"]  
reading_time: 4 minutes  

---

# Thread Lifecycle in Java

In Java, a thread goes through **five states** in its lifecycle from creation to termination. These states are managed by the Java [Virtual Machine](https://www.mindstick.com/forum/155642/how-to-create-virtual-machine-instances-in-google-cloud) (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 implements `Runnable`.

## Example

```java
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

```java
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](https://www.mindstick.com/interview/2499/what-is-the-purpose-of-synchronized-block) or resource** that is **locked** by another thread, it enters the **blocked state**.
- The thread waits until the lock is released.

## Example

```java
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](https://www.mindstick.com/forum/160913/writing-a-query-to-update-records-based-on-a-condition-in-sql-server) [occurs due](https://www.mindstick.com/forum/159540/when-stack-unwinding-occurs-due-to-an-exception-and-its-consequences) to thread [synchronization](https://www.mindstick.com/articles/11983/synchronization-in-c-sharp).

## 4. Waiting (or Timed Waiting) State

- When a thread calls `wait()`, `join()`, or `park()`, it goes into Waiting and waits indefinitely until another thread notifies it.
- When a thread calls `sleep(time)`, `join(time)`, or `wait(time)`, it goes into Timed Waiting, which means it will wait for a certain period of time.

## Example

```java
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

```java
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](https://yourviews.mindstick.com/view/81837/has-pakistan-controlled-corona-pandemic) by the JVM.
- [Understanding](https://answers.mindstick.com/qa/39151/india-has-signed-a-tripartite-memorandum-of-understanding-mou-with-which-countries-for-civil-nuclear-cooperation) these states helps in [debugging](https://www.mindstick.com/blog/393/javascript-debugging) multi-threaded programs and optimizing thread performance.
- Blocked, Waiting, and Timed Waiting states are important in synchronization and [concurrency control](https://www.mindstick.com/forum/160902/how-does-sql-server-manage-concurrency-control).

Also, read: [What is the difference between wait and sleep in java](https://www.mindstick.com/interview/33961/what-is-the-difference-between-wait-and-sleep-in-java)

---

Original Source: https://www.mindstick.com/articles/338823/thread-lifecycle-in-java

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
