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:
- ThreadGroup(String name): creates a new thread group with the given name.
- ThreadGroup(ThreadGroup parent, String name): creates a new thread group with the given name, as a child of the parent thread group.
- 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:
1 2 3 | ThreadGroup group = new ThreadGroup("My Thread Group"); |
Creating a new thread and adding it to a group:
1 2 3 4 5 6 7 8 | Thread thread = new Thread(group, new Runnable() { public void run() { // code to be executed by the thread } }); thread.start(); |
Listing all threads in a group:
1 2 3 4 5 6 7 8 | int numThreads = group.activeCount(); Thread[] threads = new Thread[numThreads]; group.enumerate(threads); for (Thread t : threads) { System.out.println(t.getName()); } |
Interrupting all threads in a group:
1 2 3 | group.interrupt(); |
Setting the maximum priority for a group of threads:
1 2 3 | group.setMaxPriority(Thread.NORM_PRIORITY + 2); |
1 2 3 | Destroying a thread group: |
1 2 3 | group.destroy(); |
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:
1 2 3 4 | ThreadGroup group = new ThreadGroup("My Thread Group"); group.list(); |
activeCount()
– To get the number of active threads in the group:
1 2 3 4 5 | ThreadGroup group = new ThreadGroup("My Thread Group"); int numThreads = group.activeCount(); System.out.println("Number of active threads: " + numThreads); |
interrupt()
– To interrupt all threads in the group:
1 2 3 4 | ThreadGroup group = new ThreadGroup("My Thread Group"); group.interrupt(); |
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):
1 2 3 4 | ThreadGroup group = new ThreadGroup("My Thread Group"); group.destroy(); |
setMaxPriority()
– To set the maximum priority for all threads in the group:
1 2 3 4 | ThreadGroup group = new ThreadGroup("My Thread Group"); group.setMaxPriority(Thread.NORM_PRIORITY + 2); |
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:
1 2 3 4 5 6 7 8 9 10 11 | ThreadGroup group = new ThreadGroup("My Thread Group"); Thread thread = new Thread(group, new Runnable() { public void run() { System.out.println("Running thread: " + Thread.currentThread().getName()); } }); thread.start(); group.setMaxPriority(Thread.NORM_PRIORITY + 2); group.list(); |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | import java.io.*; import java.lang.Thread; class ChildThread implements Runnable { Thread t; ChildThread(ThreadGroup g, String name) { t = new Thread(g, this, name); g.setMaxPriority(8); System.out.println(t.getName() + " belongs to the group: " + g.getName()); System.out.println("Maximum priority of " + g.getName() + "is: "+g.getMaxPriority()); t.start(); } public void run() { try { for (int i = 1; i <= 10; i++) { System.out.println(t.getName() + ":" + i); Thread.sleep(1000); } } catch (InterruptedException e) { System.out.println(t.getName() + " is interrupted"); } } } |
Main.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | import java.io.*; import java.lang.Thread; import java.io.*; import java.lang.Thread; public class Main { public static void main(String[] args) throws InterruptedException { ThreadGroup tg1 = new ThreadGroup("Group A"); ChildThread one = new ChildThread(tg1, "First Thread"); ChildThread two = new ChildThread(tg1, "Second Thread"); try { Thread.sleep(3000); System.out.println("All the threads in the group will be stopped"); tg1.stop(); } catch (InterruptedException e) { System.out.println("main thread is interrupted"); } tg1.list(); } } |
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | First Thread belongs to the group: Group A Maximum priority of Group Ais: 8 Second Thread belongs to the group: Group A Maximum priority of Group Ais: 8 First Thread:1 Second Thread:1 First Thread:2 Second Thread:2 First Thread:3 Second Thread:3 All the threads in the group will be stopped java.lang.ThreadGroup[name=Group A,maxpri=8] Thread[First Thread,5,Group A] Thread[Second Thread,5,Group A] |
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.