When program starts JVM creates a ThreadGroup named main. Unless specified, all newly created threads become members of the main thread group.

When Program starts JVM creates a ThreadGroup named main

package threadGroupTest0_groupName;

/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */

publicclass ThreadGroupTest {

publicstaticvoid main(String[] args) {

//When program starts JVM creates a ThreadGroup named main

System.out.println("current ThreadGroup name (created by JVM) = "

+Thread.currentThread().getThreadGroup().getName());

/* Unless specified, all newly created threads

* become members of the main ThreadGroup.

*/

Thread thread1 = new Thread("Thread-1");

Thread thread2 = new Thread("thread-2");

System.out.println("Thread-1's ThreadGroup = "

+thread1.getThreadGroup().getName());

System.out.println("Thread-2's ThreadGroup = "

+thread2.getThreadGroup().getName());

}

}

/*OUTPUT

current ThreadGroup name (created by JVM) = main

Thread-1's ThreadGroup = main

Thread-2's ThreadGroup = main

*/

When program starts JVM creates a ThreadGroup named main.

So, Thread.currentThread().getThreadGroup().getName() returned main.

We created Thread-1 and Thread-2, no ThreadGroup was specified so both threads became member of the main thread group.

ThreadGroup’s constructor >

ThreadGroup(String name)

Creates a new thread group with specified name.

The parent of this ThreadGroup is ThreadGroup of the currently running thread.

In the program in below section, we will be creating ThreadGroup named childThreadGroup1.

ThreadGroup childThreadGroup1 = new ThreadGroup("childThreadGroup1");

The parent of this childThreadGroup1 is ThreadGroup of the currently running thread i.e. main. [When program starts JVM creates a ThreadGroup named main and thread named main, where main thread belongs to main ThreadGroup]