ThreadGroup in Java: Manage Multiple Threads with Ease
Learn how to efficiently manage multiple threads in Java using the ThreadGroup class. Discover how ThreadGroup enables you to group threads, allowing easy suspension, resumption, and interruption of all threads within a group. Understand the structure of thread groups and the deprecation of certain methods like suspend(), resume(), and stop().
ThreadGroup in Java
Java lets you group multiple threads into one object. This way, you can suspend, resume, or interrupt all threads in the group with one method call.
Note: The methods suspend()
, resume()
, and stop()
are deprecated.
The ThreadGroup
class in java.lang
implements thread groups. A ThreadGroup
can include other thread groups, forming a tree structure where each group has a parent except the initial group. A thread can access info about its own group but not its parent or other groups.
Constructors of ThreadGroup
No. | Constructor | Description |
---|---|---|
1 | ThreadGroup(String name) | Creates a thread group with the given name. |
2 | ThreadGroup(ThreadGroup parent, String name) | Creates a thread group with the given parent group and name. |
Methods of ThreadGroup
No. | Method | Description |
---|---|---|
1 | void checkAccess() | Checks if the current thread can modify the group. |
2 | int activeCount() | Returns the number of active threads in the group and subgroups. |
3 | int activeGroupCount() | Returns the number of active groups in the group and subgroups. |
4 | void destroy() | Destroys the group and all its subgroups. |
5 | int enumerate(Thread[] list) | Copies every active thread in the group and subgroups into the array. |
6 | int getMaxPriority() | Returns the maximum priority of the group. |
7 | String getName() | Returns the group's name. |
8 | ThreadGroup getParent() | Returns the group's parent. |
9 | void interrupt() | Interrupts all threads in the group. |
10 | boolean isDaemon() | Checks if the group is a daemon group. |
11 | void setDaemon(boolean daemon) | Changes the daemon status of the group. |
12 | boolean isDestroyed() | Checks if the group has been destroyed. |
13 | void list() | Prints information about the group. |
14 | boolean parentOf(ThreadGroup g) | Checks if the group is the argument group or one of its ancestors. |
15 | void suspend() | Suspends all threads in the group. |
16 | void resume() | Resumes all suspended threads in the group. |
17 | void setMaxPriority(int pri) | Sets the maximum priority of the group. |
18 | void stop() | Stops all threads in the group. |
19 | String toString() | Returns a string representation of the group. |
Example: Grouping Threads
Syntax
public class ThreadGroupDemo implements Runnable {
public void run() {
System.out.println(Thread.currentThread().getName());
}
public static void main(String[] args) {
ThreadGroupDemo runnable = new ThreadGroupDemo();
ThreadGroup tg1 = new ThreadGroup("Parent ThreadGroup");
Thread t1 = new Thread(tg1, runnable, "one");
t1.start();
Thread t2 = new Thread(tg1, runnable, "two");
t2.start();
Thread t3 = new Thread(tg1, runnable, "three");
t3.start();
System.out.println("Thread Group Name: " + tg1.getName());
tg1.list();
}
}
Output
one
two
three
Thread Group Name: Parent ThreadGroup
java.lang.ThreadGroup[name=Parent ThreadGroup,maxpri=10]
Example: activeCount()
Syntax
public class ActiveCountExample {
public static void main(String[] args) {
ThreadGroup tg = new ThreadGroup("The parent group of threads");
Thread t1 = new Thread(tg, new Runnable() {
public void run() {
try { Thread.sleep(5); } catch (InterruptedException e) {}
}
}, "first");
System.out.println("Starting the first");
Thread t2 = new Thread(tg, new Runnable() {
public void run() {
try { Thread.sleep(5); } catch (InterruptedException e) {}
}
}, "second");
System.out.println("Starting the second");
System.out.println("The total number of active threads are: " + tg.activeCount());
}
}
Output
Starting the first
Starting the second
The total number of active threads are: 2
Example: activeGroupCount()
Syntax
public class ActiveGroupCountExample {
public static void main(String[] args) {
ThreadGroup tg = new ThreadGroup("The parent group of threads");
ThreadGroup tg1 = new ThreadGroup(tg, "the child group");
Thread t1 = new Thread(tg, new Runnable() {
public void run() {
try { Thread.sleep(5); } catch (InterruptedException e) {}
}
}, "the first");
System.out.println("Starting the first");
Thread t2 = new Thread(tg, new Runnable() {
public void run() {
try { Thread.sleep(5); } catch (InterruptedException e) {}
}
}, "the second");
System.out.println("Starting the second");
System.out.println("The total number of active thread groups are: " + tg.activeGroupCount());
}
}
Output
Starting the first
Starting the second
The total number of active thread groups are: 1
Example: destroy()
Syntax
public class DestroyExample {
public static void main(String[] args) {
ThreadGroup tg = new ThreadGroup("The parent group of threads");
ThreadGroup tg1 = new ThreadGroup(tg, "the child group");
Thread t1 = new Thread(tg, new Runnable() {
public void run() {
try { Thread.sleep(5); } catch (InterruptedException e) {}
}
}, "the first");
System.out.println("Starting the first");
Thread t2 = new Thread(tg, new Runnable() {
public void run() {
try { Thread.sleep(5); } catch (InterruptedException e) {}
}
}, "the second");
System.out.println("Starting the second");
tg1.destroy();
System.out.println(tg1.getName() + " destroyed");
}
}
Output
Starting the first
Starting the second
the child group destroyed