ThreadGroup in JavaLast 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:
ThreadGroup ClassThe 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 ThreadGroupThe 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 classThe 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 classThere are many methods in ThreadGroup class. A list of ThreadGroup methods is given below.
Example of Creating ThreadGroupThe 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 MethodsHere, we are going to discuss several examples of ThreadGroup Class and its methods. Example 1: Creating a ThreadGroup and Adding ThreadsThis 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 ThreadGroupThis example demonstrates how to find the number of active threads in a thread group using the activeCount() method. Output: Total active threads: 2 Next TopicShutdownhook-thread |
We request you to subscribe our newsletter for upcoming updates.