SrcForge

Lesson 41

Thread Life Cycle

A thread moves through New, Runnable, Waiting and Terminated states during its lifetime. The JVM manages these transitions to control multithreaded execution.

Java Track

Save progress as you learn.

61 lessons

Lesson content

A thread moves through a series of states from creation to completion. The JVM manages these transitions and controls how threads execute.

Definition List

  • Thread Life Cycle: The sequence of states a thread passes through during execution.
  • Thread: A lightweight process that performs a specific task within a program.
  • JVM (Java Virtual Machine): The runtime environment that manages thread execution.
  • Thread State: The current status of a thread at a specific point in time.
  • New State: The initial state of a thread after creation and before calling start().
  • Runnable State: The state in which a thread is ready to run and waiting for CPU allocation.
  • Waiting State: A state where a thread pauses execution until another thread or event signals it to continue.
  • Terminated State: The final state of a thread after execution is completed.

Stages of the Thread Life Cycle

  • New
  • Runnable
  • Waiting
  • Terminated

Thread State Flow

New → Runnable → Waiting → Runnable → Terminated

1. New State

A thread begins its life cycle in the New state. The thread object is created but start() has not been called yet.

Example 01: New State

Thread t1 = new Thread();

2. Runnable State

After calling start(), the thread enters the Runnable state. It is eligible for execution and waiting for CPU time from the scheduler.

Example 02: Runnable State

t1.start();

3. Waiting State

A thread enters the Waiting state when it must pause for another thread or resource. Common causes include calling wait(), join(), or waiting for resource availability.

Example 03: Waiting State

thread.join();

4. Terminated State

A thread enters the Terminated state after completing its task. Once terminated, it cannot be restarted.

Example 04: Terminated State

public void run() {
    System.out.println("Task completed");
}

Full Thread State Transition Example

class MyThread extends Thread {

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

    public static void main(String[] args) {

        MyThread t1 = new MyThread();

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

        t1.start();

        System.out.println("State after start(): " + t1.getState());
    }
}

Output

State after creation: NEW
State after start(): RUNNABLE
Thread is running...

Best Practices

  • Avoid unnecessary waiting — keep waiting periods short.
  • Use synchronization carefully to avoid excessive blocking.
  • Monitor thread states using getState() when debugging.
  • Properly handle interruptions and thread-related exceptions.

FAQ

1. What are the main states in the Java thread life cycle?

The primary states are New, Runnable, Waiting and Terminated.

2. What happens when start() is called?

The thread moves from New to Runnable, making it eligible for execution.

3. Can a terminated thread be restarted?

No. Once a thread reaches the Terminated state, it cannot be started again.

A thread moves through New, Runnable, Waiting and Terminated states.
The JVM scheduler controls when Runnable threads execute.
Calling start() transitions a thread from New to Runnable.
A Waiting thread resumes only after a notification or event.
A Terminated thread cannot be restarted.
Use getState() to monitor thread states during debugging.
Proper synchronization prevents deadlocks and resource conflicts.

Where Java is used

Java in real-world development

New State

Thread object created. start() not yet called. Thread is not eligible for execution.

Runnable State

Thread is ready to run. JVM scheduler decides when it gets CPU time.

Waiting State

Thread pauses execution. Caused by wait(), join(), or resource unavailability.

Terminated State

Thread has finished execution. Resources released. Cannot be restarted.

getState() Method

Returns the current thread state. Useful for debugging multithreaded applications.

Thread Synchronization

Protects shared resources. Avoid excessive blocking to prevent deadlocks.

Prev: MultithreadingBack to hub