Java Program to Create a Group of a Thread

You are currently viewing Java Program to Create a Group of a Thread

Java program to create a group of a thread

// Java program to create a group of a thread

class MyThread extends Thread {
  MyThread(String threadname, ThreadGroup tg) {
    super(tg, threadname);
    start();
  }
  public void run() {
    System.out.println(Thread.currentThread().getName() + " is running");
  }
}

public class Main {
  public static void main(String[] args) {
    try {
      ThreadGroup group = new ThreadGroup("Parent thread");

      MyThread t1 = new MyThread("Child Thread1", group);
      MyThread t2 = new MyThread("Child Thread2", group);
      MyThread t3 = new MyThread("Child Thread3", group);

    } catch (Exception e) {
      System.out.println(e);
    }
  }
}

Output

Child Thread1 is running
Child Thread3 is running
Child Thread2 is running

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