Java如何检查线程组是否已被销毁?
可以使用ThreadGroup.isDestroyed()方法检查线程组及其子组是否已被销毁。
package org.nhooo.example.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
};
// 销毁“父”组及其所有子组
parent.destroy();
for (ThreadGroup tg : groupArray) {
if (tg.isDestroyed()) {
System.out.println(tg.getName() + " is destroyed");
} else {
System.out.println(tg.getName() + " is not destroyed");
}
}
}
}结果是:
GrandParent is not destroyed Uncle is not destroyed Parent is destroyed Son is destroyed Daughter is destroyed Neighbour is not destroyed
热门推荐
10 对患者生日祝福语简短
11 结婚祝福语简短装备
12 周岁祝福语学生文案简短
13 订婚领证祝福语简短精辟
14 导师获奖祝福语大全简短
15 新婚购房祝福语简短精辟
16 牛年祝福语简短的爱人
17 送芒果的祝福语简短
18 送给学长毕业祝福语简短