Java Thread Lifecycle Complete Beginners Guide

Java Thread Lifecycle Complete Beginners Guide

Introduction

In Java, threads live an exciting life! ???? From being born (New ????), getting ready (Runnable ????), springing into action (Running ⚡), sometimes getting stuck (Blocked ????), and finally retiring (Dead ⚰️) — each stage shows how Java keeps apps smooth and multitasking sharp

Thread Class and Runnable Interface

???? Using the Thread Class

  • You create a class that extends Thread.
  • Override the run() method with the code that the thread should execute.
  • Start the thread using start().

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


public class Main {
    public static void main(String[] args) {
        MyThread t = new MyThread();
        t.start(); // Starts the thread
    }
}

???? Using the Runnable Interface

  • You create a class that implements Runnable.
  • Pass the instance to a Thread object.
  • Start the thread using start().
class MyRunnable implements Runnable {
    public void run() {
        System.out.println("Thread is running...");
    }
}

public class Main {
    public static void main(String[] args) {
        Thread t = new Thread(new MyRunnable());
        t.start(); // Starts the thread
    }
}

Life Cycle Transitions (State Changes)

Life Cycle Transitions

Image Source: google

From State To State Cause of Transition
New Runnable Thread.start() is called
Runnable Running Thread is selected by CPU scheduler
Running Blocked/Waiting Thread waits for a resource or lock
Blocked/Waiting Runnable Resource becomes available or notified
Running Terminated Thread completes or is forcefully stopped
Runnable Terminated Thread is killed before it runs

Explore Java services in Hexadecimal Software 

Creating a Thread (New State)

When a thread is created in Java, it enters the New state. This means the thread object has been instantiated but has not started executing yet.

There are two main ways to create a thread:

 1. Extending the Thread class

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

public class Main {
    public static void main(String[] args) {
        MyThread t = new MyThread(); // Thread is created (New State)
        // t is now in New state
    }
}

 2. Implementing the Runnable interface

java
class MyRunnable implements Runnable {
    public void run() {
        System.out.println("Thread is running...");
    }
}

public class Main {
    public static void main(String[] args) {
        Runnable r = new MyRunnable();
        Thread t = new Thread(r); // Thread is created (New State)
        // t is in New state
    }
}

For Hire Java Developer Services

Starting a Thread (Runnable State)

Once a thread is created (New state), calling the start() method moves it to the Runnable state. This means the thread is ready to run but waiting for CPU time to be scheduled by the thread scheduler.

 Example Runnable State

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

public class Main {
    public static void main(String[] args) {
        MyThread t = new MyThread(); // New state
        t.start(); // Now in Runnable state
    }
}

???? What happens in Runnable state?

  • The thread is now eligible to run, but it does not start immediately.
  • The Thread Scheduler decides when the thread actually gets CPU time to move into the Running state.
  • It may stay in Runnable state for some time depending on CPU availability and thread priority.

Running State Thread Execution

 Example Running State

class MyThread extends Thread {
    public void run() {
        System.out.println("Thread is running..."); // This runs in Running state
    }
}

public class Main {
    public static void main(String[] args) {
        MyThread t = new MyThread(); // New
        t.start(); // Runnable → Running (when CPU picks it)
    }
}

⚙️ Key Points:

  • Only one thread per core can be in the Running state at a time.
  • Other threads remain in Runnable, waiting for their turn.
  • The actual switch from Runnable to Running is controlled by the JVM thread scheduler, not the programmer.

For the Mobile App Development Services

WRITTEN BY:- HEXADECIMAL SOFTWARE and HEXAHOME

What's Your Reaction?

like

dislike

love

funny

angry

sad

wow