SrcForge

Lesson 48

ThreadGroup

ThreadGroup organizes multiple threads as a single unit for collective management.

Java Track

Save progress as you learn.

61 lessons

Lesson content

ThreadGroup is a Java class that groups related threads so they can be managed collectively.

Definition List

  • ThreadGroup: A class representing a collection of related threads.
  • Thread: A lightweight unit of execution in a Java program.
  • Active Thread: A thread that has started and is running or ready.
  • Enumeration: The process of listing all threads in a group.
  • Thread Management: Controlling and monitoring thread execution.

Creating a ThreadGroup

// Create a thread group
ThreadGroup myGroup = new ThreadGroup("MyThreadGroup");

// Create threads inside the group
Thread thread1 = new Thread(myGroup, new MyRunnable(), "Thread1");
Thread thread2 = new Thread(myGroup, new MyRunnable(), "Thread2");

ThreadGroup Hierarchy

System Thread Group

        ├── Main Thread Group
        │       ├── Thread1
        │       └── Thread2

        └── Custom Thread Group
                ├── Thread3
                └── Thread4

Example: Java ThreadGroup

package com.java.Multi_threading;

public class ThreadGroupExample {

    public static void main(String[] args) {

        ThreadGroup myGroup = new ThreadGroup("MyThreadGroup");

        Thread thread1 = new Thread(myGroup, new MyRunnable_1(), "Thread1");
        Thread thread2 = new Thread(myGroup, new MyRunnable_1(), "Thread2");

        thread1.start();
        thread2.start();

        System.out.println("ThreadGroup Name: " + myGroup.getName());
        System.out.println("Number of Threads in the ThreadGroup: " + myGroup.activeCount());

        Thread[] threads = new Thread[myGroup.activeCount()];
        myGroup.enumerate(threads);

        for (Thread t : threads) {
            System.out.println("Thread: " + t.getName());
        }
    }
}

class MyRunnable_1 implements Runnable {

    @Override
    public void run() {
        System.out.println("Executing thread: " + Thread.currentThread().getName());
    }
}

Output

Executing thread: Thread1
Executing thread: Thread2
ThreadGroup Name: MyThreadGroup
Number of Threads in the ThreadGroup: 2
Thread: Thread1
Thread: Thread2

Common ThreadGroup Methods

// Get the group name
myGroup.getName();

// Estimated number of active threads
myGroup.activeCount();

// Copy active threads into an array
myGroup.enumerate(threadArray);

// Interrupt all threads in the group
myGroup.interrupt();

// Get the parent thread group
myGroup.getParent();

Best Practices

  • Use ThreadGroup for logical organization of related threads.
  • Use activeCount() and enumerate() for debugging.
  • Prefer ExecutorService for new projects.
  • Keep thread group structures simple.

FAQ

1. What is a ThreadGroup in Java?

A class used to organize and manage multiple threads as a single unit.

2. How do you create a ThreadGroup?

Use the ThreadGroup constructor: new ThreadGroup("MyGroup").

3. Is ThreadGroup still used in modern Java?

It is still available but ExecutorService is preferred for modern applications.

ThreadGroup groups related threads for collective management.
Pass the group as the first Thread constructor argument.
activeCount() returns an estimated active thread count.
enumerate() copies active threads into an array.
interrupt() stops all threads in the group at once.
ThreadGroups support a parent-child hierarchy.
Prefer ExecutorService over ThreadGroup in modern code.

Where Java is used

Java in real-world development

ThreadGroup

A class that represents a collection of related threads managed as one unit.

getName()

Returns the name assigned to the thread group.

activeCount()

Returns an estimated count of active threads in the group.

enumerate()

Copies all active threads in the group into a Thread array.

interrupt()

Sends an interrupt signal to every thread in the group.

ThreadGroup vs ExecutorService

ThreadGroup organizes threads; ExecutorService manages task execution with reuse and scalability.

Prev: Thread PoolBack to hub