← Back to Chapters

Thread Life Cycle

? Thread Life Cycle

? Quick Overview

In Java, a Thread goes through multiple states from creation to termination. This journey is called the Thread Life Cycle. Understanding this life cycle is essential for writing efficient multithreaded applications.

? Key Concepts

  • Thread is a lightweight process
  • Multiple threads can run concurrently
  • Thread states are managed by the JVM
  • State changes depend on methods like start(), sleep(), and join()

? Interactive Thread Simulation

NEW
RUNNABLE
RUNNING
WAITING
TERMINATED
Current State: NEW (Thread created)

? Syntax / Theory

A thread in Java passes through the following main states:

  • New – Thread object is created
  • Runnable – Thread is ready to run
  • Running – Thread is executing
  • Blocked / Waiting – Thread is paused
  • Terminated – Thread execution ends

? Code Example

? View Code Example
// Demonstrates basic thread life cycle
class MyThread extends Thread {
public void run() {
// Code inside run() represents the Running state
System.out.println("Thread is running");
}
}

public class ThreadLifeCycle {
public static void main(String[] args) {
// Thread is in New state
MyThread t = new MyThread();

// Thread enters Runnable state
t.start();
}
}

? Live Output / Explanation

Output

Thread is running

Explanation: The thread is first created (New), then started using start(), after which the JVM calls the run() method and the thread enters the Running state.

? Tips & Best Practices

  • Always use start() instead of calling run() directly
  • Keep thread tasks small and efficient
  • Avoid unnecessary thread creation
  • Use proper synchronization when sharing resources

? Try It Yourself

  • Create two threads and run them simultaneously
  • Use sleep() to observe the Waiting state
  • Experiment with thread priorities