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 祝福语签名简短大全女生