This Java tutorial helps you understand the use of ThreadGroup with some code examples.

ThreadGroup is a convenient class that groups some related threads as a single unit and allows you to perform some operations on a group as a whole, rather than with each separate thread.

You need to specify the name of the group upon creation like this:

ThreadGroup groupA = new ThreadGroup("Group A");
ThreadGroup groupB = new ThreadGroup("Group B");
And when you create a new thread, specify the thread group to which the thread belongs using the following Thread constructors:

      Thread(ThreadGroup group, String name)

Thread(ThreadGroup group, Runnable target)

Thread(ThreadGroup group, Runnable target, String name)

For example, suppose Task is a thread, you can create 4 threads and group them in one group like this:

ThreadGroup group = new ThreadGroup("GroupA");

new Task(group, "A").start();
new Task(group, "B").start();
new Task(group, "C").start();
new Task(group, "D").start();
For more complex need, you can also create a tree of thread groups using the following ThreadGroup constructor:

ThreadGroup(ThreadGroup parent, String name)
For example, the following code creates a tree consisting of 2 groups:

ThreadGroup base = new ThreadGroup("Base");
ThreadGroup group1 = new ThreadGroup(base, "Group1");
ThreadGroup group2 = new ThreadGroup(base, "Group2");
The ThreadGroup class provides several convenient methods that work on all threads at once, here to name a few:



 

Let’s see an example in action. Consider the following class:

class Task extends Thread {
	public Task(ThreadGroup threadGroup, String name) {
		super(threadGroup, name);
	}

	public void run() {
		boolean running = true;

		while (running) {
			try {
				System.out.println(getName() + " is running");
				Thread.sleep(1000);
			} catch (InterruptedException ex) {
				running = false;
				System.out.println(getName() + " is interrupted and then terminates");
			}
		}
	}
}
As you can see in the run() method, this thread will runs forever until it is interrupted by another thread. And here is the test program:

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

		new Task(group, "A").start();
		new Task(group, "B").start();
		new Task(group, "C").start();
		new Task(group, "D").start();

		Thread.sleep(10000);

		group.interrupt();

	}
}
As you can see, 4 threads are created and added to one thread group and they are all started to run concurrently. The main thread calls interrupt() on the group after the program has been running for 10 seconds:

Thread.sleep(10000);
group.interrupt();
This results in all threads in the group are interrupted, and by intention of the code, the threads terminate, hence the following output repeats 10 times:

A is running
D is running
B is running
C is running
and the last output looks like this:

C is interrupted and then terminates
B is interrupted and then terminates
A is interrupted and then terminates
D is interrupted and then terminates
 

API References:

 

Related Java Thread Tutorials:

 

Other Java Concurrency Tutorials:


About the Author:

is certified Java programmer (SCJP and SCWCD). He started programming with Java in the time of Java 1.4 and has been falling in love with Java since then. Make friend with him on Facebook and watch his Java videos you YouTube.