Java Program to Demonstrate the Thread.Join() Method

You are currently viewing Java Program to Demonstrate the Thread.Join() Method

Java program to demonstrate the Thread.join() method

// Java program to demonstrate Thread.join() method

class MyThread implements Runnable {
  public void run() {
    try {
      System.out.println(Thread.currentThread().getName() + " is alive: " + Thread.currentThread().isAlive());
    } catch (Exception e) {

    }
  }
}

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

      t.start();

      //Waits for 500ms this thread to die.
      t.join(500);

      System.out.println(t.getName() + " is alive: " + t.isAlive());
    } catch (Exception e) {

    }
  }
}

Output

Thread-0 is alive: true
Thread-0 is alive: false

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