Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

public class Main {

    public static void main(String[] args) {
        ThreadGroupDemo tg = new ThreadGroupDemo();

    }
}

class ThreadGroupDemo implements Runnable {
    public ThreadGroupDemo() {
        ThreadGroup pGroup = new ThreadGroup("Parent ThreadGroup");

        ThreadGroup cGroup = new ThreadGroup(pGroup, "Child ThreadGroup");

        Thread t1 = new Thread(pGroup, this);
        System.out.println("Starting " + t1.getName());
        t1.start();

        Thread t2 = new Thread(cGroup, this);
        System.out.println("Starting " + t2.getName());
        t2.start();

        Thread[] list = new Thread[pGroup.activeCount()];
        int count = pGroup.enumerate(list, true);
        for (int i = 0; i < count; i++) {
            System.out.println("Thread " + list[i].getName() + " found");
        }

    }

    public void run() {
        System.out.println(Thread.currentThread().getName() + " finished executing.");
    }
}