线程组
可以把某一线程归属到线程组中,线程组中既可以有线程,也可以有线程组,组中还可以有线程,组织结构跟数类似。
线程组的作用是,可以批量的管理线程或线程组对象,有效对线程或线程组对象进行组织。
线程组常用方法
1.ThreadGroup构造函数
//指定线程组的名字,默认挂在当前线程组下(例如:在main方法里执行改方法,就是挂在main线程组下)
//这是线程组的自动归属特性
public ThreadGroup(String name) {
this(Thread.currentThread().getThreadGroup(), name);
}
//指定父线程组以及指定线程组名字
public ThreadGroup(ThreadGroup parent, String name) {
this(checkParentAccess(parent), parent, name);
}
//无参构造函数private修饰,外部不可调用,生成system线程组,其为所有线程组的最顶端父类线程组
private ThreadGroup() { // called from C code
this.name = "system";
this.maxPriority = Thread.MAX_PRIORITY;
this.parent = null;
}
2.activeCount()方法是递归获取以该线程组为根节点的下面所有活动线程的估计数。
3.activeGroupCount()方法是递归获取以该线程组为根节点的下面所有线程组的估计数。
4.int enumerate(Thread list[])方法默认是执行int enumerate(Thread list[],true)方法,递归获取以该线程组为根节点的下面所有线程复制到指定数组。
int enumerate(Thread list[],false)方法是将该线程组下子线程复制到指定数组不递归,不复制子孙线程。
以上方法示例:
public class ThreadGroupDemo {
public static class MyThread extends Thread{
@Override
public void run() {
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
//根线程组a
ThreadGroup threadGroup = new ThreadGroup("a");
//根线程组a子线程组a1
ThreadGroup threadGroup1 = new ThreadGroup(threadGroup,"a1");
//根线程组a子线程组a2
ThreadGroup threadGroup2 = new ThreadGroup(threadGroup,"a2");
//线程组a2子线程组a21
ThreadGroup threadGroup21 = new ThreadGroup(threadGroup2,"a21");
//根线程组a下线程
Thread threada = new Thread(threadGroup,new MyThread(),"a下线程");
//线程组a1下线程
Thread threada1 = new Thread(threadGroup1,new MyThread(),"a1下线程");
//线程组a2下线程
Thread threada2 = new Thread(threadGroup2,new MyThread(),"a2下线程");
//根线程组a21下线程
Thread threada21 = new Thread(threadGroup21,new MyThread(),"a21下线程");
threada.start();
threada1.start();
threada2.start();
threada21.start();
System.out.println("根线程组a中活动线程估计数:" + threadGroup.activeCount());
System.out.println("根线程组a中活动线程组估计数:" + threadGroup.activeGroupCount());
Thread[] threads1 = new Thread[10];
Thread[] threads2 = new Thread[10];
//递归a线程组下所有线程复制到threads1数组
threadGroup.enumerate(threads1);
//非递归,仅将a线程组下线程复制到threads2数组
threadGroup.enumerate(threads2,false);
for(Thread thread : threads1){
if(thread == null){
continue;
}
System.out.println("threads1内线程:" + thread.getName());
}
for(Thread thread : threads2){
if(thread == null){
continue;
}
System.out.println("threads2内线程:" + thread.getName());
}
}
}
根线程组a中活动线程估计数:4
根线程组a中活动线程组估计数:3
threads1内线程:a下线程
threads1内线程:a1下线程
threads1内线程:a2下线程
threads1内线程:a21下线程
threads2内线程:a下线程
5.interrupt()方法 是递归获取以该线程组为根节点的下面所有活动线程进行全部线程中断。
6.ThreadGroup getParent() 方法是获取该线程组的父级线程组,最顶部的父级线程组是system线程组。
组内线程批量停止
public class ThreadGroupDemo {
public static class MyThread extends Thread{
@Override
public void run() {
try {
System.out.println(Thread.currentThread().getName()+"开始死循环!");
while (!Thread.currentThread().isInterrupted()){
}
System.out.println(Thread.currentThread().getName()+"结束死循环!");
} catch (Exception e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
//根线程组a
ThreadGroup threadGroup = new ThreadGroup("a");
//根线程组a子线程组a1
ThreadGroup threadGroup1 = new ThreadGroup(threadGroup,"a1");
//根线程组a子线程组a2
ThreadGroup threadGroup2 = new ThreadGroup(threadGroup,"a2");
//线程组a2子线程组a21
ThreadGroup threadGroup21 = new ThreadGroup(threadGroup2,"a21");
//根线程组a下线程
Thread threada = new Thread(threadGroup,new MyThread(),"a下线程");
//线程组a1下线程
Thread threada1 = new Thread(threadGroup1,new MyThread(),"a1下线程");
//线程组a2下线程
Thread threada2 = new Thread(threadGroup2,new MyThread(),"a2下线程");
//根线程组a21下线程
Thread threada21 = new Thread(threadGroup21,new MyThread(),"a21下线程");
threada.start();
threada1.start();
threada2.start();
threada21.start();
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
//执行interrupt()方法,将所有活动线程的isInterrupted状态置为true
threadGroup.interrupt();
System.out.println("调用interrupt方法()");
}
}
a下线程开始死循环!
a1下线程开始死循环!
a21下线程开始死循环!
a2下线程开始死循环!
调用interrupt方法()
a21下线程结束死循环!
a2下线程结束死循环!
a1下线程结束死循环!
a下线程结束死循环!