Java Thread Lifecycle

Introduction

Multithreading is one of the most powerful features of Java that allows multiple tasks to run concurrently. Every thread created in Java passes through a series of predefined states during its execution. These states are collectively known as the Java Thread Lifecycle.

Understanding the lifecycle of a thread is essential for writing efficient, responsive, and error-free multithreaded applications. Whether you are developing desktop applications, web servers, or enterprise systems, knowing how threads move between different states helps you manage synchronization, performance, and resource utilization.

What is Java Thread Lifecycle?

The Java Thread Lifecycle is the sequence of states that a thread goes through from its creation until it finishes execution.

A thread does not start executing immediately after it is created. Instead, it moves through various states depending on the operations performed on it and the scheduling decisions made by the JVM.

Java defines the lifecycle using the Thread.State enumeration.

The six thread states are:

  1. NEW
  2. RUNNABLE
  3. BLOCKED
  4. WAITING
  5. TIMED_WAITING
  6. TERMINATED

These states help developers understand what a thread is doing at any point in time.

Java Thread Lifecycle Diagram


            +---------+
            |  NEW    |
            +---------+
                 |
             start()
                 |
                 v
          +-------------+
          | RUNNABLE    |
          +-------------+
          /      |      \
         /       |       \
 BLOCKED WAITING TIMED_WAITING
         \       |       /
          \      |      /
           +-------------+
           | RUNNABLE    |
           +-------------+
                 |
          Execution Ends
                 |
                 v
          +--------------+
          | TERMINATED   |
          +--------------+

Java Thread States

1. NEW State

A thread is in the NEW state when it is created but has not yet started.

Example:


Thread t = new Thread();

At this point:

  • Memory is allocated.
  • Thread object exists.
  • Thread has not started execution.

The thread enters the RUNNABLE state only after calling:


t.start();

2. RUNNABLE State

When the start() method is called, the thread enters the RUNNABLE state.

Example:


Thread t = new Thread();
t.start();

In this state:

  • The thread is ready to execute.
  • It waits for CPU scheduling.
  • The JVM thread scheduler decides when it gets CPU time.

A thread may remain in the RUNNABLE state for some time before actually executing.

3. BLOCKED State

A thread enters the BLOCKED state when it is waiting to acquire a monitor lock.

Example:


synchronized(object) {
    // critical section
}

If another thread already owns the lock, the current thread becomes BLOCKED until the lock becomes available.

Characteristics:

  • Waiting for a synchronized lock.
  • Cannot execute until the lock is released.
  • Automatically returns to RUNNABLE after obtaining the lock.

4. WAITING State

A thread enters the WAITING state when it waits indefinitely for another thread to perform a particular action.

Common methods:


wait()
join()
LockSupport.park()

Example:


thread.join();

The thread waits until another thread completes.

Characteristics:

  • Waits indefinitely.
  • Requires notification.
  • Returns to RUNNABLE after receiving notification.

5. TIMED_WAITING State

A thread enters the TIMED_WAITING state when it waits for a specified period.

Methods causing this state:


sleep()
wait(timeout)
join(timeout)
parkNanos()
parkUntil()

Example:


Thread.sleep("1111");

Here the thread sleeps for 3 seconds.

After the timeout expires, it automatically returns to the RUNNABLE state.

6. TERMINATED State

A thread reaches the TERMINATED state after its execution finishes.

Example:


public void run() {
    System.out.println("Thread Finished");
}

After the run() method completes, the thread cannot be restarted.

Attempting to restart it results in an exception.


thread.start();
thread.start();

Output:

Exception in thread “main”
java.lang.IllegalThreadStateException

Syntax

Creating a thread:


class MyThread extends Thread {
    public void run() {
        System.out.println("Running...");
    }
    public static void main(String[] args) {
        MyThread t = new MyThread();
        System.out.println(t.getState());
        t.start();
        System.out.println(t.getState());
    }
}

Example: Checking Thread State


class Demo extends Thread {

    public void run() {

        try {
            Thread.sleep("111");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println("Thread is running");
    }

    public static void main(String[] args) {

        Demo t = new Demo();

        System.out.println("State after creation: " + t.getState());

        t.start();

        System.out.println("State after start: " + t.getState());
        try {
            Thread.sleep("110");
            System.out.println("Current State: " + t.getState());
            t.join();
        } catch (Exception e) {
        }
        System.out.println("Final State: " + t.getState());
    }
}

Possible Output

State after creation: NEW
State after start: RUNNABLE
Current State: TIMED_WAITING
Thread is running
Final State: TERMINATED

Example Using getState()


class StateExample extends Thread {
    public void run() {
        System.out.println(getState());
    }
    public static void main(String[] args) {
        StateExample t = new StateExample();
        System.out.println(t.getState());
        t.start();
    }
}

Output:

NEW
RUNNABLE
Current State Action Next State
NEW start() RUNNABLE
RUNNABLE Waiting for lock BLOCKED
RUNNABLE wait() WAITING
RUNNABLE sleep() TIMED_WAITING
WAITING notify() RUNNABLE
TIMED_WAITING Timeout ends RUNNABLE
BLOCKED Lock acquired RUNNABLE
RUNNABLE run() completes TERMINATED

Real-Life Example

Imagine a customer visiting a bank.

  • NEW – Customer enters the bank.
  • RUNNABLE – Customer stands in the queue.
  • BLOCKED – Another customer is currently at the counter.
  • WAITING – Customer waits for a token announcement.
  • TIMED_WAITING – Customer waits during the lunch break for 15 minutes.
  • TERMINATED – Customer completes the transaction and leaves the bank.

This analogy makes the Java Thread Lifecycle much easier to understand.

Why Understanding Thread Lifecycle is Important

Knowing thread states helps developers:

  • Improve application performance.
  • Detect deadlocks.
  • Debug synchronization issues.
  • Avoid race conditions.
  • Optimize CPU usage.
  • Build scalable multithreaded applications.
  • Write safer concurrent code.

Common Mistakes

1. Calling run() Instead of start()

Incorrect:


thread.run();

Correct:


thread.start();

Calling run() directly executes the method on the current thread instead of creating a new thread.

2. Restarting a Thread

Incorrect:


thread.start();
thread.start();

A thread can only be started once.

3. Ignoring InterruptedException

Methods like sleep(), wait(), and join() throw InterruptedException. Always handle or propagate it properly.

4. Using sleep() for Synchronization

Using Thread.sleep() to coordinate threads is unreliable. Prefer synchronization tools such as wait(), notify(), locks, or utilities from java.util.concurrent.

5. Forgetting Synchronization

Accessing shared resources without synchronization can lead to inconsistent results and race conditions.

Best Practices

  • Always use start() instead of directly calling run().
  • Keep thread tasks short and focused.
  • Use thread pools instead of creating excessive threads.
  • Synchronize shared resources carefully.
  • Handle interruptions gracefully.
  • Avoid busy waiting.
  • Prefer modern concurrency utilities from java.util.concurrent.
  • Use getState() only for debugging or monitoring, not for controlling application logic.

Conclusion

The Java Thread Lifecycle describes how a thread progresses from creation to completion. Every thread begins in the NEW state, becomes RUNNABLE after start() is called, may transition through BLOCKED, WAITING, or TIMED_WAITING depending on synchronization and timing operations, and finally reaches the TERMINATED state when execution finishes.

A solid understanding of these states enables developers to build efficient, responsive, and reliable multithreaded applications. Mastering the thread lifecycle also makes it easier to debug concurrency issues, improve performance, and answer Java interview questions with confidence.

Java Thread Lifecycle – Interview Questions

Q 1: What is the thread lifecycle in Java?
Ans: The thread lifecycle represents different states of a thread from creation to termination.
Q 2: Name the states of thread lifecycle.
Ans: New, Runnable, Running, Waiting/Blocked, and Terminated.
Q 3: What is the New state?
Ans: A thread is created but not yet started.
Q 4: What happens in Runnable state?
Ans: The thread is ready to run and waiting for CPU time.
Q 5: What is the Terminated state?
Ans: The thread has finished execution.

Java Thread LifeCycle – Objective Questions (MCQs)

Q1. Which of the following is not a valid state in the Java thread lifecycle?






Q2. In which state is a thread after the start() method is called but before it begins execution?






Q3. Which method is used to move a thread from the Running state to the Waiting state?






Q4. When does a thread enter the Terminated (or Dead) state?






Q5. Which of the following transitions is not possible in the Java thread lifecycle?






Related Java Tutorials