Clean • Professional
A Thread Group in Java is a container that holds multiple threads and allows you to manage them as a single unit.
It simplifies multi-threaded application management by enabling bulk operations, monitoring, and organization of threads.
Treat multiple threads as one entity for bulk operations and monitoring.
Thread groups are useful to:

A Thread Group is created using the ThreadGroup class. It allows you to group multiple threads together so they can be managed as a single unit.
Basic Syntax
ThreadGroupgroup=newThreadGroup("MyThreadGroup");
"MyThreadGroup" is the name of the thread group.main thread group).ThreadGroupparentGroup=newThreadGroup("ParentGroup");
ThreadGroupchildGroup=newThreadGroup(parentGroup,"ChildGroup");
ChildGroup becomes a subgroup of ParentGroup.System.out.println(childGroup.getParent().getName());
Output:
ParentGroup
ThreadGroup group = new ThreadGroup("WorkerGroup");
Thread t1 = new Thread(group, () -> {
System.out.println(Thread.currentThread().getName() + " is running");
}, "Worker-1");
Thread t2 = new Thread(group, () -> {
System.out.println(Thread.currentThread().getName() + " is running");
}, "Worker-2");
t1.start();
t2.start();
System.out.println(t1.getThreadGroup().getName());
Output:
WorkerGroup
👉 Explanation
t1.getThreadGroup() and t1.getThreadGroup().getName() , both refer to the same thread group:
t1.getThreadGroup() returns the ThreadGroup object to which the thread belongs.t1.getThreadGroup().getName() returns the name of that ThreadGroup.
getName()
System.out.println(group.getName());
getParent()
ThreadGroupparent= group.getParent();
activeCount()
intcount= group.activeCount();
activeGroupCount()
intsubGroups= group.activeGroupCount();
enumerate(Thread[] list)
Thread[] threads =newThread[group.activeCount()];
group.enumerate(threads);
interrupt()
group.interrupt();
setMaxPriority(int p)
group.setMaxPriority(Thread.NORM_PRIORITY);
Example tree structure:
System ThreadGroup
└── ParentGroup
├── ChildGroup1
└── ChildGroup2
ThreadGroupgroup=newThreadGroup("Workers");
Runnabletask= () -> {
System.out.println(Thread.currentThread().getName() +" started");
try {
Thread.sleep(2000);
}catch (InterruptedException e) {
System.out.println(Thread.currentThread().getName() +" interrupted");
}
System.out.println(Thread.currentThread().getName() +" finished");
};
Threadt1=newThread(group, task,"Worker-1");
Threadt2=newThread(group, task,"Worker-2");
t1.start();
t2.start();
// Interrupt all threads in the group after 1 second
newThread(() -> {
try {
Thread.sleep(1000);
group.interrupt();
}catch (InterruptedException e) {
e.printStackTrace();
}
}).start();
Sample Output:
Worker-1 started
Worker-2 started
Worker-1 interrupted
Worker-2 interrupted
Thread groups allow you to set an uncaught exception handler for all threads in the group:
group.setUncaughtExceptionHandler((t, e) -> {
System.out.println(t.getName() +" threw exception: " + e.getMessage());
});
ExecutorService or ThreadPoolExecutor for efficient management.