April 1, 2024

Thread Group in Java

All threads belong to one of a thread group in Java. When you create a thread in Java it is put into a thread group specified either by you or to the same group as the thread that created it if no thread group is explicitly specified.

Default thread group

When a Java application is started one thread starts running immediately which is known as main thread in Java and this main thread belongs to a thread group called main. If you create other threads (with in the context of main thread) with out specifying thread group then these thread will also belong to main thread group.

Usage of thread group in Java

Thread groups provide a convenient way to manage multiple threads as a single thread group object. Using that thread group object you can manipulate all the threads belonging to that group as a single unit rather than doing that individually. For example-


This will interrupt all the thread belonging to that thread group with a single statement.

ThreadGroup Class in Java

Java provides a class java.lang.ThreadGroup for creating thread groups in Java.

ThreadGroup Class Constructors

ThreadGroup class has two constructors for creating thread groups.

  • public ThreadGroup(String name)- Constructs a new thread group. The parent of this new group is the thread group of the currently running thread.
  • public ThreadGroup(ThreadGroup parent, String name)- Creates a new thread group. The parent of this new group is the specified thread group.
Methods in ThreadGroup Class

Some of the important methods of the ThreadGroup Class are.

  • String getName()- Returns the name of this thread group.
  • ThreadGroup getParent()- Returns the parent of this thread group.
  • boolean isDaemon()- Tests if this thread group is a daemon thread group.
  • void checkAccess()- Determines if the currently running thread has permission to modify this thread group.
  • int activeCount()- Returns an estimate of the number of active threads in this thread group and its subgroups. Recursively iterates over all subgroups in this thread group.
  • int enumerate(Thread[] list)- Copies into the specified array every active thread in this thread group and its subgroups.
  • void interrupt()- Interrupts all threads in this thread group.
  • void list()- Prints information about this thread group to the standard output.

Creating Thread in specific thread group

Thread class in Java provides constructor where you can specify a thread group. Few of those constructors are listed here.

  • public Thread(ThreadGroup group, Runnable target)
  • public Thread(ThreadGroup group, String name)
  • public Thread(ThreadGroup group, Runnable target, String name)

Thread group Java example

public class TGDemo implements Runnable {
  public static void main(String[] args) {
    System.out.println("In main method " + Thread.currentThread().getName() 
        + " Group " + Thread.currentThread().getThreadGroup().getName());
    TGDemo runnableTarget = new TGDemo();
    // Creating two thread groups
    ThreadGroup group1 = new ThreadGroup("Group-1");
    ThreadGroup group2 = new ThreadGroup("Group-2");
    // Creating 4 threads belonging to the created thread groups
    Thread t1 = new Thread(group1, runnableTarget, "T1");
    Thread t2 = new Thread(group1, runnableTarget, "T2");
    Thread t3 = new Thread(group2, runnableTarget, "T3");
    Thread t4 = new Thread(group2, runnableTarget, "T4");

  public void run() {
    System.out.println("In run method " + Thread.currentThread().getName() 
        + " Group " + Thread.currentThread().getThreadGroup().getName());
    if (Thread.interrupted()) {
      System.out.println("interrupted " + Thread.currentThread().getName());
In main method main Group main
In run method T1 Group Group-1
In run method T2 Group Group-1
In run method T4 Group Group-2
interrupted T1
interrupted T2
In run method T3 Group Group-2

As you can see initially when the application starts main thread starts running and it belongs to thread group main.

Then two thread groups "Group-1" and "Group-2" are created. Four thread are also created to of these threads belong to “Group-1” and other two belongs to “Group-2”.

In run() method you can see specific logic is there for threads belonging to "Group-1".

Reference- https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/ThreadGroup.html

That's all for the topic Thread Group in Java. If something is missing or you have something to share about the topic please write a comment.

You may also like

No comments:

Post a Comment