ThreadGroup

ThreadGroup

In this tutorial, we are going to discuss about ThreadGroup. In Java, ThreadGroup is a class that represents a group of threads. It provides a way to organize threads into a hierarchical structure, which can be useful for managing and controlling threads collectively.

Based on functionality we can group threads as a single unit which is nothing but ThreadGroup. ThreadGroup provides a convenient way to perform common operations for all threads belongs to a particular group.

ThreadGroup

We can create a ThreadGroup by using the following constructors

ThreadGroup g = new ThreadGroup(String gName);

We can attach a Thread to the ThreadGroup by using the following constructor of Thread class

Thread t = new Thread(ThreadGroup g, String name);
ThreadGroup tgroup = new ThreadGroup("Printing Threads");
MyThread t1=new MyThread(tgroup,"Header Printing");
MyThread t2=new MyThread(tgroup,"Footer Printing");
MyThread t3=new MyThread(tgroup,"Body Printing");
-----------
tgroup.stop();
How ThreadGroup works in Java?
  • A Thread Group is a collection of multiple related threads. Thread group allows developers to handle multiple java threads simultaneously and is available in java.lang package.
  • Internally thread group can be thought of as a tree in which each thread has a parent, except the parent thread that does not have a parent associated with it.
  • It is to be noted that a thread belonging to a particular thread group has access to information about the same thread group to which the thread belongs; it does not have any information about its parent thread group or any other thread group.

Here are some key aspects of ThreadGroup in Java:

  1. Hierarchy: Thread groups form a hierarchical tree structure, where each thread group can contain other thread groups as well as individual threads. This hierarchy allows for logical organization and management of threads based on their functionalities or responsibilities.
  2. Common Properties and Behaviors: Thread groups share common properties and behaviors, such as setting the daemon status, maximum priority, and uncaught exception handler. When you set properties on a thread group, they apply to all threads within that group.
  3. Exception Handling: ThreadGroup provides a mechanism for handling uncaught exceptions that occur in threads within the group. You can set an uncaught exception handler for a thread group, which will be invoked whenever a thread within the group throws an uncaught exception.
  4. Concurrency Utilities Integration: ThreadGroup integrates with Java’s concurrency utilities, such as executors and thread pools. You can specify a ThreadGroup when creating an executor or a thread pool, allowing you to manage groups of related threads collectively.
  5. Security Considerations: Thread groups are used in security management to control access to resources and operations performed by threads within the group. Security policies can be applied at the thread group level to restrict certain actions based on security permissions.

Here’s an example demonstrating how to create and use a ThreadGroup in Java:

public class ThreadGroupExample {
    public static void main(String[] args) {
        ThreadGroup group = new ThreadGroup("MyThreadGroup");

        Thread thread1 = new Thread(group, () -> {
            // Thread 1 logic
            System.out.println("Thread 1 running");
        });

        Thread thread2 = new Thread(group, () -> {
            // Thread 2 logic
            System.out.println("Thread 2 running");
        });

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

        System.out.println("Thread group active count: " + group.activeCount());
        System.out.println("Thread group name: " + group.getName());
    }
}

In this example, a new ThreadGroup named “MyThreadGroup” is created. Two threads, thread1 and thread2, are then assigned to this thread group. When the threads are started, they execute their respective logic. Finally, the active count and name of the thread group are printed to the console.

Note

Java Multi Threading concept is implementing by using the following 2 methods :

  1. GreenThread Model
  2. Native OS Model
GreenThread Model

The threads which are managed completely by JVM without taking support for underlying OS, such type of threads are called Green Threads.

Native OS Model
  • The Threads which are managed with the help of underlying OS are called Native Threads.
  • Windows based OS provide support for Native OS Model
  • Very few OS like SunSolaries provide support for GreenThread Model
  • Anyway GreenThread model is deprecated and not recommended to use.

That’s all about ThreadGroup in java. If you have any queries or feedback, please write us at contact@waytoeasylearn.com. Enjoy learning, Enjoy Java.!!

ThreadGroup
Scroll to top