ThreadGroup in Java

Last Updated : 20 Jan 2026

A ThreadGroup in Java is used to group multiple threads into a single unit so they can be managed together. In this chapter, you will learn what a ThreadGroup is, why it is used in Java, how to create and manage thread groups, and how ThreadGroup helps in controlling and monitoring multiple threads.

What is ThreadGroup in Java?

A ThreadGroup is a built-in class that allows you to group multiple threads into a single unit so they can be managed together.

In simple words, instead of handling threads one by one, ThreadGroup helps you organize and control related threads as a group.

Key points about ThreadGroup are:

  • A ThreadGroup represents a collection of threads.
  • It is useful for managing, monitoring, and controlling multiple threads at once.
  • Operations like interrupting, suspending, or checking active threads can be applied to the entire group.
  • Thread groups can contain sub-groups that forms parent–child hierarchy.
  • Every thread belongs to some ThreadGroup (by default, the main thread group).

ThreadGroup Class

The ThreadGroup class is provided in the java.lang package and is used to organize multiple threads into a single group.

A ThreadGroup represents a collection of related threads. It can also contain other thread groups, which allows thread groups to be arranged in a parent–child hierarchy. This hierarchy forms a tree structure, where every thread group (except the main thread group) has a parent thread group.

A thread can access information about its own thread group, but it is not allowed to access information about its parent thread group or any other thread groups.

Importing ThreadGroup

The ThreadGroup class belongs to the java.lang package. Since java.lang is imported automatically by Java, you do not need to explicitly import the ThreadGroup class. You can directly use ThreadGroup in your program without any import statement.

Constructors of ThreadGroup class

The ThreadGroup class provides only two constructors:

1. ThreadGroup(String name)

This constructor creates a new thread group with the specified name.

Syntax:

Here is the syntax of the constructor:

2. ThreadGroup(ThreadGroup parent, String name)

This constructor creates a new thread group with the specified name and assigns it to the given parent thread group.

Syntax:

Here is the syntax of the constructor:

Methods of ThreadGroup class

There are many methods in ThreadGroup class. A list of ThreadGroup methods is given below.

Modifier and TypeMethodDescription
voidcheckAccess()This method determines if the currently running thread has permission to modify the thread group.
intactiveCount()This method returns an estimate of the number of active threads in the thread group and its subgroups.
intactiveGroupCount()This method returns an estimate of the number of active groups in the thread group and its subgroups.
voiddestroy()This method destroys the thread group and all of its subgroups.
intenumerate(Thread[] list)This method copies into the specified array every active thread in the thread group and its subgroups.
intgetMaxPriority()This method returns the maximum priority of the thread group.
StringgetName()This method returns the name of the thread group.
ThreadGroupgetParent()This method returns the parent of the thread group.
voidinterrupt()This method interrupts all threads in the thread group.
booleanisDaemon()This method tests if the thread group is a daemon thread group.
voidsetDaemon(boolean daemon)This method changes the daemon status of the thread group.
booleanisDestroyed()This method tests if this thread group has been destroyed.
voidlist()This method prints information about the thread group to the standard output.
booleanparentOf(ThreadGroup gThis method tests if the thread group is either the thread group argument or one of its ancestor thread groups.
voidsuspend()This method is used to suspend all threads in the thread group.
voidresume()This method is used to resume all threads in the thread group which was suspended using suspend() method.
voidsetMaxPriority(int pri)This method sets the maximum priority of the group.
voidstop()This method is used to stop all threads in the thread group.
StringtoString()This method returns a string representation of the Thread group.

Example of Creating ThreadGroup

The following example shows how to create a ThreadGroup and assign threads to it.

Output:

Thread running: Thread-1
Thread running: Thread-2

More Examples of ThreadGroup Class and Its Methods

Here, we are going to discuss several examples of ThreadGroup Class and its methods.

Example 1: Creating a ThreadGroup and Adding Threads

This example shows how to create a thread group and add multiple threads to it.

Output:

one
two
three
Thread Group Name: Parent ThreadGroup
java.lang.ThreadGroup[name=Parent ThreadGroup,maxpri=10]

Example 2: Using activeCount() Method in ThreadGroup

This example demonstrates how to find the number of active threads in a thread group using the activeCount() method.

Output:

Total active threads: 2