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 婆婆高寿祝福语大全简短