Understanding Thread Group in Java
- Details
- Written by Nam Ha Minh
- Last Updated on 12 August 2019   |   Print Email
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:
- activeCount(): returns an estimate of the number of active threads in the thread group and its subgroups.
- activeGroupCount(): returns an estimate of the number of active groups in the thread group and its subgroups.
- destroy(): destroys the thread group and all of its subgroups.
- enumerate(Thread[] list): copies into the specified array every active thread in this thread group and its subgroups.
- getMaxPriority(): returns the maximum priority of the thread group.
- interrupt(): interrupts all threads in the thread group.
- isDaemon(): tests if the thread group is a daemon thread group.
- setMaxPriority(int priority): sets the maximum priority of the group.
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 runningand 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:
- How to use Threads in Java (create, start, pause, interrupt and join)
- How to list all threads currently running in Java
- Understanding Thread Priorities and Daemon Thread in Java
- Understanding Thread States (Thread Life Cycle) in Java
Other Java Concurrency Tutorials:
- Java Synchronization Tutorial
- Understanding Deadlock, Livelock and Starvation with Code Examples in Java
- Understanding Java Fork-Join Framework with Examples
- Understanding Java Thread Pool and Executors
Comments
Would request you to try to provide some live examples where it is used..
Thanks a Lot...