ThreadGroup的基本操作
1、ThreadGroup的基本方法
activeCount()用于获取group中活跃的线程,这只是个估计值,并不能百分之百的保证数字一定正确(比如在调用后,某个线程结束了生命周期或者新的线程加如了进来都会导致数据的不准确),该方法会递归获取其他子group中的活跃线程
activeGroupCount() 用于获取group中活跃的子group,这也是一个近似估值,该方法也会递归获取所有的子group
getMaxPriority()用于获取group的优先级,默认情况下,Group的优先级为10,在该group中,所有线程的优先级都不能大于group的优先级
getName()用于获取group的名字
getParent()用于获取group的父group,如果父group不存在,则会返回null,比如system group的父group就为null
list()该方法没有返回值,执行该方法会将group中的所有的活跃线程信息全部输出到控制台,也就是System.out
parentOf( Threadgroup g) 会判断当前group是不是给定group的父group,另外如果给定的group就是自己本身,那么该方法也会返回true
setMaxPriority( int pri) 会指定group的最大优先级,最大优先级不能超过父group的最大优先级,执行该方法不仅会改变当前group的最大优先级,还会改变所有子group的最大优先级
下面是java的测试代码:
public class ThreadGroupBasic {
public static void main(String[] args) throws InterruptedException {
/**
* create a thread group and thread.
*/
ThreadGroup group = new ThreadGroup("group1");
Thread thread = new Thread(group, () -> {
while (true) {
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}, "thread");
thread.setDaemon(true);//设置为守护线程
thread.start();
//make sure the thread is started
TimeUnit.MILLISECONDS.sleep(1);
ThreadGroup mainGroup = Thread.currentThread().getThreadGroup();
System.out.println("activeCount="+mainGroup.activeCount());
System.out.println("activeGroupCount="+mainGroup.activeGroupCount());
System.out.println("getMaxPriority="+mainGroup.getMaxPriority());
System.out.println("getName="+mainGroup.getName());
System.out.println("getParent="+mainGroup.getParent());
mainGroup.list();
System.out.println("---------------");
System.out.println("parentOf="+mainGroup.parentOf(group));
System.out.println("parentOf="+mainGroup.parentOf(mainGroup));
}
}
下面是测试结果:
2、ThreadGroup的interrupt
/**
* interrupt 一个thread group 会导致该group中所有的active线程都被interrupt,也就是
* 说该group中每一个线程的interrupt标识都被设置了
*
*/
public class ThreadGroupInterrupt {
public static void main(String[] args) throws InterruptedException {
ThreadGroup group = new ThreadGroup("TestGroup");
new Thread(group,()->{
while (true){
try {
TimeUnit.MILLISECONDS.sleep(2);
} catch (InterruptedException e) {
//received interrupt SIGNAL and clear quickly
break;
}
}
System.out.println("t1 will exit.");
},"t1").start();
new Thread(group,()->{
while (true){
try {
TimeUnit.MILLISECONDS.sleep(1);
} catch (InterruptedException e) {
//received interrupt SIGNAL and clear quickly
break;
}
}
System.out.println("t2 will exit.");
},"t2").start();
//make sure all of above threads started.
TimeUnit.MILLISECONDS.sleep(2);
group.interrupt();
}
}
运行结果如下:
上面的代码足够简单,从运行结果中可以看出,group中的active thread都将被interrupt
3、ThreadGroup的destroy
destroy用于销毁ThreadGroup,该方法只针对一个没有任何active线程的group进行一次destroy标记,调用该方法的直接结果是在父group中将自己移除
public class ThreadGroupDestroy {
public static void main(String[] args) {
ThreadGroup group = new ThreadGroup("TestGroup");
ThreadGroup mainGroup = Thread.currentThread().getThreadGroup();
System.out.println("group.isDestroyed="+group.isDestroyed());
mainGroup.list();
group.destroy();
System.out.println("group.isDestroyed="+group.isDestroyed());
mainGroup.list();
}
}
程序运行结果如下: