Monday, April 6, 2026

Explain Thread Lifecycle in Java

Introduction

In multithreaded applications, developers often struggle to understand how threads behave at runtime. This leads to issues like deadlocks, performance bottlenecks, and unpredictable execution.

👉 Direct Answer: The thread lifecycle in Java consists of multiple states—New, Runnable, Running, Blocked/Waiting, and Terminated—through which a thread transitions during its execution, managed by the JVM scheduler.




What is Thread Lifecycle?

The thread lifecycle defines the various states a thread goes through from creation to termination.

In my decade of teaching Java, I’ve noticed that understanding these states deeply is critical for writing efficient multithreaded applications.


Thread Lifecycle States

 Main States of a Thread:

  • New

  • Runnable

  • Running

  • Blocked / Waiting / Timed Waiting

  • Terminated (Dead)


Diagram Explanation (Conceptual Flow)

New → Runnable → Running → Waiting/Blocked → Runnable → Terminated

1. New State

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

class MyThread extends Thread {
    public void run() {
        System.out.println("Thread running");
    }
}

public class NewStateExample {
    public static void main(String[] args) {
        MyThread t = new MyThread(); // New state
    }
}

Expert Annotation

  • Thread object is created

  • start() is not yet called

 Edge Case

  • Calling start() twice → IllegalThreadStateException


2. Runnable State

After calling start(), thread moves to Runnable state.

public class RunnableStateExample {
    public static void main(String[] args) {
        Thread t = new Thread(() -> {
            System.out.println("Running...");
        });

        t.start(); // Moves to Runnable state
    }
}

 Expert Annotation

  • Thread is ready to run

  • Waiting for CPU allocation

 Edge Case

  • Runnable ≠ Running (depends on scheduler)


3. Running State

Thread is actively executing.

public class RunningExample {
    public static void main(String[] args) {
        Thread t = new Thread(() -> {
            System.out.println(Thread.currentThread().getName() + " is running");
        });

        t.start();
    }
}

 Expert Annotation

  • CPU is assigned

  • run() method is executing

 Edge Case

  • No direct control over when thread enters running state


4. Blocked / Waiting / Timed Waiting

Thread enters these states when waiting for resources or signals.

public class WaitingExample {
    public static void main(String[] args) throws InterruptedException {
        Thread t = new Thread(() -> {
            try {
                Thread.sleep(2000); // Timed Waiting
                System.out.println("Woke up");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });

        t.start();
    }
}

 Expert Annotation

  • sleep() → Timed Waiting

  • wait() → Waiting

  • Lock contention → Blocked

 Edge Case

  • Forgetting notify() → thread stuck forever


5. Terminated State

Thread finishes execution.

public class TerminatedExample {
    public static void main(String[] args) {
        Thread t = new Thread(() -> {
            System.out.println("Task done");
        });

        t.start();
    }
}

 Expert Annotation

  • Thread completes execution

  • Cannot be restarted

 Edge Case

  • Calling start() again → Exception


Example 6: Full Lifecycle Demonstration

public class FullLifecycle {
    public static void main(String[] args) throws InterruptedException {

        Thread t = new Thread(() -> {
            try {
                Thread.sleep(1000);
                System.out.println("Thread executing");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });

        System.out.println(t.getState()); // NEW
        t.start();

        System.out.println(t.getState()); // RUNNABLE
        Thread.sleep(500);

        System.out.println(t.getState()); // TIMED_WAITING
        Thread.sleep(1500);

        System.out.println(t.getState()); // TERMINATED
    }
}

 Expert Annotation

  • Demonstrates actual state transitions

  • Useful for debugging

 Edge Case

  • Timing may vary due to scheduler


Key Points to Remember

 Important Observations

  • Thread lifecycle is managed by JVM

  • Scheduler decides execution order

  • States are dynamic


Real-Time Scenarios

Our students in Hyderabad often face these issues:

  • Threads stuck in waiting state

  • Deadlocks due to improper synchronization

  • Performance issues due to blocked threads


Common Mistakes Developers Make

  • Calling run() instead of start()

  • Ignoring thread states

  • Improper synchronization

  • Not handling interruptions


Thread States Comparison Table



Best Practices

✔ Follow These:

  • Use ExecutorService instead of raw threads

  • Avoid unnecessary thread creation

  • Handle interruptions properly


Advanced Insight (From Experience)

In enterprise systems:

  • Thread lifecycle understanding helps in performance tuning

  • Used in high-concurrency applications

  • Critical for microservices and backend systems

In my experience, mastering thread lifecycle is essential for becoming a strong Java developer.


Learn More (Recommended)

This is one of the Best AI powered Core JAVA Online Training in 2026, helping developers become industry-ready.


Key Takeaways

  • Thread goes through multiple states

  • JVM controls lifecycle

  • Understanding states is crucial for debugging


FAQ

1. What are the main thread states in Java?

New, Runnable, Running, Waiting/Blocked, and Terminated.

2. Can we restart a thread?

No, once terminated, it cannot be restarted.

3. What is the difference between Runnable and Running?

Runnable means ready, Running means executing.

4. What causes blocked state?

Waiting for a lock.

5. How to manage threads efficiently?

Use ExecutorService instead of manual threads.


Final Thoughts

Understanding the thread lifecycle is a fundamental skill for Java developers working on real-world applications. It helps you write efficient, scalable, and bug-free multithreaded code.

No comments:

Post a Comment

To build frictionless production-ready Java applications in 2026, developers must move beyond traditional coding styles and adopt modern pra...