In Java, ThreadGroup
is a class that represents a group of threads. It provides a way to organize threads into a hierarchical structure for easier management and control.
This is particularly valuable in situation in which you want to suspend and resume a number of related threads.
In addition, a thread group can also include other thread groups. The thread groups form a tree in which every thread group except the initial thread group has a parent.
A thread is allowed to access information about its own thread group, but not to access information about its thread group’s parent thread group or any other thread groups.
Creating ThreadGroup
You can create a ThreadGroup
by specifying its name and optionally the parent ThreadGroup
. If no parent is specified, the current thread’s ThreadGroup
is used as the parent.
- 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.
public class ThreadGroupExample {
public static void main(String[] args) {
// Create a parent thread group
ThreadGroup parentGroup = new ThreadGroup("ParentGroup");
// Create child threads within the parent thread group
Thread thread1 = new Thread(parentGroup, new MyRunnable(), "Thread1");
Thread thread2 = new Thread(parentGroup, new MyRunnable(), "Thread2");
// Start the threads
thread1.start();
thread2.start();
// Display active thread count in the parent group
System.out.println("Active threads in parent group: " + parentGroup.activeCount());
// List threads in the parent group
Thread[] threads = new Thread[parentGroup.activeCount()];
parentGroup.enumerate(threads);
System.out.println("Threads in parent group:");
for (Thread thread : threads) {
System.out.println("- " + thread.getName());
}
}
// Runnable implementation
static class MyRunnable implements Runnable {
@Override
public void run() {
System.out.println("Thread " + Thread.currentThread().getName() + " is running.");
try {
Thread.sleep(1000); // Simulate some work
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
Methods
- int activeCount(): This method returns the number of threads in the group plus any group for which this thread is parent.
import java.lang.*;
class NewThread extends Thread
{
NewThread(String threadname, ThreadGroup tgob)
{
super(tgob, threadname);
start();
}
public void run()
{
for (int i = 0; i < 1000; i++)
{
try
{
Thread.sleep(10);
}
catch (InterruptedException ex)
{
System.out.println("Exception encounterted");
}
}
}
}
public class ThreadGroupDemo
{
public static void main(String arg[])
{
// creating the thread group
ThreadGroup gfg = new ThreadGroup("parent thread group");
NewThread t1 = new NewThread("one", gfg);
System.out.println("Starting one");
NewThread t2 = new NewThread("two", gfg);
System.out.println("Starting two");
// checking the number of active thread
System.out.println("number of active thread: "
+ gfg.activeCount());
}
}
- int activeGroupCount(): This method returns an estimate of the number of active groups in this thread group.
// Java code illustrating activeGroupCount() method
import java.lang.*;
class NewThread extends Thread
{
NewThread(String threadname, ThreadGroup tgob)
{
super(tgob, threadname);
start();
}
public void run()
{
for (int i = 0; i < 1000; i++)
{
try
{
Thread.sleep(10);
}
catch (InterruptedException ex)
{
System.out.println("Exception encounterted");
}
}
System.out.println(Thread.currentThread().getName() +
" finished executing");
}
}
public class ThreadGroupDemo
{
public static void main(String arg[]) throws InterruptedException
{
// creating the thread group
ThreadGroup gfg = new ThreadGroup("gfg");
ThreadGroup gfg_child = new ThreadGroup(gfg, "child");
NewThread t1 = new NewThread("one", gfg);
System.out.println("Starting one");
NewThread t2 = new NewThread("two", gfg);
System.out.println("Starting two");
// checking the number of active thread
System.out.println("number of active thread group: "
+ gfg.activeGroupCount());
}
}
- void interrupt() : Interrupts all threads in the thread group.
public class Main {
public static void main(String[] args) throws InterruptedException {
ThreadGroup group = new ThreadGroup("MyThreadGroup");
Thread thread1 = new Thread(group, () -> {
while (!Thread.currentThread().isInterrupted()) {
System.out.println("Thread 1 is running");
}
System.out.println("Thread 1 interrupted");
});
Thread thread2 = new Thread(group, () -> {
while (!Thread.currentThread().isInterrupted()) {
System.out.println("Thread 2 is running");
}
System.out.println("Thread 2 interrupted");
});
thread1.start();
thread2.start();
Thread.sleep(1000);
group.interrupt(); // Interrupt all threads in the group
}
}
- void list() : Prints information about the thread group to the standard output.
public class Main {
public static void main(String[] args) {
ThreadGroup group = new ThreadGroup("MyThreadGroup");
Thread thread1 = new Thread(group, () -> {
System.out.println("Thread 1 is running");
});
Thread thread2 = new Thread(group, () -> {
System.out.println("Thread 2 is running");
});
thread1.start();
thread2.start();
group.list(); // Print information about the thread group
}
}
- void setMaxPriority(int priority): Sets the maximum priority that a thread in the thread group can have.. Threads in the thread group that already have a higher priority are not affected.
public class Main {
public static void main(String[] args) {
ThreadGroup group = new ThreadGroup("MyThreadGroup");
group.setMaxPriority(Thread.MAX_PRIORITY); // Set maximum priority
Thread thread1 = new Thread(group, () -> {
System.out.println("Thread 1 Priority: " + Thread.currentThread().getPriority());
});
Thread thread2 = new Thread(group, () -> {
System.out.println("Thread 2 Priority: " + Thread.currentThread().getPriority());
});
thread1.setPriority(Thread.MAX_PRIORITY); // Set thread priority
thread2.setPriority(Thread.NORM_PRIORITY); // Set thread priority
thread1.start();
thread2.start();
}
}