πŸŽ‰ Special Offer !    Code: GET300OFF    Flat β‚Ή300 OFF on every Java Course
Grab Deal πŸš€

Thread Life Cycle  


Introduction

  • The Thread Life Cycle in Java represents the different states a thread goes through β€” from creation to termination.
    • New β†’ Runnable β†’ Running β†’ Waiting/Blocked β†’ Dead
  • It shows how a thread is managed by the JVM and the Thread Scheduler, depending on operations like start(), sleep(), wait(), or when the thread finishes execution.
  • Understanding the life cycle helps developers control thread execution and write efficient multi-threaded programs.
  • There are total 5 states in thread life-cycle:-
    1. New (Created)
    2. Runnable (Ready)
    3. Running (Execution)
    4. Non-Runnable (Inactive)
    5. Terminated (Dead)

1. New (Created) State
  • In this state, a thread object is created but not yet started.
  • The thread is not alive until start() is called.
  • MyThread t1 = new MyThread(); // thread created, but not started
    If you directly call run() instead of start(), it executes as a normal method, not a separate thread.
2. Runnable (Ready) State
  • After calling start(), the thread moves to Runnable.
  • The thread is ready but waiting for CPU allocation.
  • t1.start(); // moves from New -> Runnable state
    Thread is managed by the Thread Scheduler.
3. Running (Execution) State
  • In this state, the Thread Scheduler selects a thread from Runnable and gives it CPU.
  • The thread executes its run() method.
  • public void run()
    {
        System.out.println("Thread is running...");
    }
4. Non-Runnable (Inactive) State
  • In this state, the thread is temporarily inactive but still alive.
  • It can occur due to wait(), sleep(), join(), or waiting for a lock.
  • Example:
    Thread.sleep(2000); // timed waiting
    obj.wait();        // waiting
    
5. Dead (Terminated) State
  • In this state, run() method finishes and the thread enters Dead state.
  • Once the thread has completed its task, it cannot be restarted again using start().
Thread Life Cycle in Java

Summary of Thread Life Cycle
Thread State Description How to Enter This State
1. New Thread is created but not yet started. Thread object is instantiated using new Thread().
2. Runnable Thread is ready to run and waiting for CPU allocation. Calling start() on the thread.
3. Running Thread is currently executing its run() method. Thread scheduler allocates CPU to the thread.
4. Non-Runnable Thread is currently in inactive state. Calling wait(), sleep() etc methods.
5. Terminated Thread has finished execution or has been stopped. run() method completes execution.