Java Program to Check Thread Is Alive or Not

You are currently viewing Java Program to Check Thread Is Alive or Not

Java program to check thread is alive or not

// Java program to check thread is 
// alive or not

class MyThread implements Runnable {
  public void run() {
    try {
      System.out.println("Thread is running.");
    } catch (Exception e) {

    }
  }
}

public class Main {
  public static void main(String[] args) {
    Thread t = new Thread(new MyThread());

    System.out.println("Thread isAlive: " + t.isAlive());
    t.start();
    System.out.println("Thread isAlive: " + t.isAlive());
  }
}

Output

Thread isAlive: false
Thread is running.
Thread isAlive: true

Ranjith

Hi, I'm Manoj a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Reply