In Java, every thread belongs to a group of threads.
The JDK class java.lang.ThreadGroup provides methods to handle
a whole group of Threads.
With the help of these methods we can interrupt all threads of a
group or set the maximum priority of all threads of a group.
So a thread group is used for taking collective actions on a group of
threads
xxxxxxxxxx
import java.lang.*;
class NewThread extends Thread
{
NewThread(String threadname, ThreadGroup tgob)
{
super(tgob, threadname);
start();
}
public void run()
{
for (int i = 0; i < 10; i++)
{
try
{
Thread.sleep(10);
}
catch (InterruptedException ex)
{
System.out.println("Thread " + Thread.currentThread().getName()
+ " interrupted");
}
}
System.out.println(Thread.currentThread().getName() +
" finished executing");
}
}
public class ThreadGroupDemo
{
public static void main(String arg[]) throws InterruptedException,
SecurityException, Exception
{
// creating the thread group
ThreadGroup gfg = new ThreadGroup("Parent thread");
ThreadGroup gfg_child = new ThreadGroup(gfg, "child thread");
// daemon status is set to true
gfg.setDaemon(true);
// daemon status is set to true
gfg_child.setDaemon(true);
NewThread t1 = new NewThread("one", gfg);
System.out.println("Starting " + t1.getName());
NewThread t2 = new NewThread("two", gfg);
System.out.println("Starting " + t2.getName());
// string equivalent of the parent group
System.out.println("String equivalent: " + gfg.toString());
}
}
https://www.geeksforgeeks.org/java-lang-threadgroup-class-java/