Programming Code Examples

How the Threads can be Grouped in Java7 min read

In this article we will learn what is a thread group? How to work with thread groups in Java along with example program.

In Java, threads can be grouped using the ThreadGroup class. A ThreadGroup represents a group of threads, and provides methods for controlling the group as a whole, such as the ability to interrupt all threads in the group or to set the priority of all threads in the group. Threads can be added to a ThreadGroup using the Thread constructors that take a ThreadGroup as an argument or using the add(Thread) method.




A ThreadGroup is a collection of threads that can be managed as a single unit. It provides methods for controlling the group as a whole, such as the ability to interrupt all threads in the group, set the maximum priority of all threads in the group, and check if any thread in the group is still running. Threads can be added to a ThreadGroup using the Thread constructors that take a ThreadGroup as an argument or using the add(Thread) method. Additionally, a ThreadGroup can also contain other ThreadGroups, allowing for nested thread group structures.

Here are some of the constructors available in the ThreadGroup class:

  1. ThreadGroup(String name): creates a new thread group with the given name.
  2. ThreadGroup(ThreadGroup parent, String name): creates a new thread group with the given name, as a child of the parent thread group.
  3. ThreadGroup(ThreadGroup parent, String name, boolean isDaemon): creates a new thread group with the given name, as a child of the parent thread group and with the specified daemon status.

Additionally, the ThreadGroup class also has methods like list(), activeCount(), interrupt(), destroy(), setMaxPriority() etc. which can be used to manage the group of threads.

  • getName() – To get the name of the thread group
  • setMaxPriority() – To set the maximum priority of all the threads in the group
  • setMinPriority() – To set the minimum priority of all the threads in the group
  • start() – To start the execution of all the threads in the group
  • list() – To print information of the thread group and the threads in the group.

Here are a few examples of how to use the ThreadGroup class in Java:

Creating a new thread group:

Creating a new thread and adding it to a group:

Listing all threads in a group:

Interrupting all threads in a group:

Setting the maximum priority for a group of threads:

Note that: destroy() method is a deprecated method from Java 1.2. It is not recommended to use this method because it will stop all thread and release the resources.

Here are some examples of using the list(), activeCount(), interrupt(), destroy(), and setMaxPriority() methods of the ThreadGroup class in Java:

list() – To print information of the thread group and the threads in the group:

activeCount() – To get the number of active threads in the group:

interrupt() – To interrupt all threads in the group:

destroy() – To destroy the thread group and all threads in the group (this method is deprecated from Java 1.2 and it’s not recommended to use this method):

setMaxPriority() – To set the maximum priority for all threads in the group:

Example that demonstrates creating a new thread group, creating a new thread and adding it to the group, setting the maximum priority for the group, and listing the information of the thread group and its threads:

In this example, a new thread group named “My Thread Group” is created. A new thread is created and added to the group using the Thread constructor that takes a ThreadGroup as an argument. The thread is then started using the start() method. The maximum priority for the group is set to the normal priority plus 2 using the setMaxPriority() method. Finally, the information of the thread group and its threads is printed using the list() method.

When the code runs it will print the information of the thread group and the thread that is in it.

Example, Below program demonstrates the use of thread groups in Java:

ChildThread.java

Main.java

Output:

This code creates a class named “ChildThread” that implements the Runnable interface, which means it has a run() method that can be executed by a thread. The class has a constructor that takes a ThreadGroup and a String as parameters, where the ThreadGroup will be the group of the thread and the String will be the name of the thread.

In the constructor, a new Thread is created, and it is added to the ThreadGroup passed as a parameter, the thread is also given the this runnable and the name parameter. Then the maximum priority for the thread group is set to 8, and some information about the thread and the group is printed to the console. Finally, the thread is started using the start() method.

The run() method of the ChildThread class simply loops 10 times, each time printing the name of the thread and the current iteration number, and then sleeping for 1 second.

The driver class has a main method that creates a new ThreadGroup named “Group A” and creates two new ChildThread objects, passing the ThreadGroup and a name as parameters. Then the main thread sleeps for 3 seconds and stops all threads in the ThreadGroup using the stop() method and finally prints the information about the threads in the group using the list() method.

Please note that, The stop() method of ThreadGroup is a deprecated method from Java 1.2. It is not recommended to use this method because it will stop all thread abruptly and release the resources, which can cause data inconsistencies or other unexpected behavior.

Take your time to comment on this article.

Add comment