You can use ThreadGroup.isDestroyed()
method to check whether a thread group and its subgroups has been destroyed.
package org.kodejava.lang;
public class CheckGroupDestroy {
public static void main(String[] args) {
ThreadGroup grandParent = new ThreadGroup("GrandParent");
ThreadGroup uncle = new ThreadGroup(grandParent, "Uncle");
ThreadGroup parent = new ThreadGroup(grandParent, "Parent");
ThreadGroup son = new ThreadGroup(parent, "Son");
ThreadGroup daughter = new ThreadGroup(parent, "Daughter");
ThreadGroup neighbour = new ThreadGroup("Neighbour");
ThreadGroup[] groupArray = {
grandParent, uncle, parent, son, daughter, neighbour
};
// Destroy 'parent' group and all its subgroups
parent.destroy();
// Check whether the group is destroyed. The result is,
// GrandParent, Uncle, and Neighbour did not destroyed
// because they are not Parent's subgroups
for (ThreadGroup tg : groupArray) {
if (tg.isDestroyed()) {
System.out.println(tg.getName() + " is destroyed");
} else {
System.out.println(tg.getName() + " is not destroyed");
}
}
}
}
The result is:
GrandParent is not destroyed
Uncle is not destroyed
Parent is destroyed
Son is destroyed
Daughter is destroyed
Neighbour is not destroyed
Latest posts by Wayan (see all)
- How do I get number of each day for a certain month in Java? - September 8, 2024
- How do I get operating system process information using ProcessHandle? - July 22, 2024
- How do I sum a BigDecimal property of a list of objects using Java Stream API? - July 22, 2024