「这是我参与11月更文挑战的第2天,活动详情查看:2021最后一次更文挑战」
复制线程组中的线程及子线程组
enumerate(Thread[] list) 把当前线程组和子线程组中所有的线程复制到参数数组中。
enumerate(Thread[ ] list, boolean recursive) , 如果第二个参数设置为false,则只复制当前线程组中所有的线程,不复制子线程组中的线程。
enumerate(ThreadGroup[ ] list) 把当前线程组和子线程组中所有的线程组复制到参数数组中。
enumerate(ThreadGroup[ ] list, boolean recurse) 第二个参数设置false,则只复制当前线程组的子线程组。
package com.wkcto.threadgroup;
/**
* 演示复制线程组中的内容
*/
public class Test03 {
public static void main(String[] args) {
ThreadGroup mainGroup = Thread.currentThread().getThreadGroup(); //返回main线程的main线程组
//main线程组中定义了两个子线程组
ThreadGroup group1 = new ThreadGroup("group1"); //默认group1的父线程组就是当前线程组main
ThreadGroup group2 = new ThreadGroup(mainGroup, "group2");
Runnable r = new Runnable() {
@Override
public void run() {
while (true){
System.out.println("----当前线程: " + Thread.currentThread());
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
//创建并启动三个线程
Thread t1 = new Thread(r, "t1"); //默认在main线程组中创建线程
Thread t2 = new Thread(group1, r, "t2"); //在group1线程组中创建线程
Thread t3 = new Thread(group2, r, "t3"); //在group2线程组中创建线程
t1.start();
t2.start();
t3.start();
//1) 把main线程组中的线程复制到数组中
//先定义存储线程的数组,数组的长度为main线程组中活动线程的数量
Thread[] threadList = new Thread[mainGroup.activeCount()];
/* //把main线程组包括子线程组中的所有的线程复制到数组中
mainGroup.enumerate(threadList);
//遍历threadList数组
for (Thread thread : threadList) {
System.out.println(thread);
}
System.out.println("----------------------------");*/
//只把main线程组中的线程复制到数组中,不包含子线程组的线程
mainGroup.enumerate(threadList, false);
//遍历threadList数组
for (Thread thread : threadList) {
System.out.println(thread);
}
System.out.println("----------------------------");
//2) 把main线程组中的子线程组复制到数组中
//定义数组存储线程组
ThreadGroup [] threadGroups = new ThreadGroup[mainGroup.activeGroupCount()];
//把main线程组中的子线程组复制到数组中
mainGroup.enumerate(threadGroups);
System.out.println("============================");
for (ThreadGroup threadGroup : threadGroups) {
System.out.println(threadGroup);
}
}
}
线程组的批量中断
线程组的interrupt() 可以给该线程组中所有的活动线程添加中断标志。
package com.wkcto.threadgroup;
/**
* 线程组的批量中断
*/
public class Test04 {
public static void main(String[] args) throws InterruptedException {
Runnable r = new Runnable() {
@Override
public void run() {
System.out.println("当前线程--" + Thread.currentThread() + "--开始循环");
//当线程没有被中断就一直循环
while ( !Thread.currentThread().isInterrupted()){
System.out.println(Thread.currentThread().getName() + "------------------");
/* try {
Thread.sleep(500);
} catch (InterruptedException e) {
//如果中断睡眠中的线程,产生中断异常, 同时会清除中断标志
e.printStackTrace();
}*/
}
System.out.println(Thread.currentThread().getName() + "循环结束");
}
};
//创建线程组
ThreadGroup group = new ThreadGroup("group");
//在group线程组中创建5个线程
for (int i = 0; i < 5; i++) {
new Thread(group,r).start();
}
//main线程睡眠2秒
Thread.sleep(50);
//中断线程组, 会中断线程组中所有的线程
group.interrupt();
}
}
设置守护线程组
守护线程是为其他线程提供服务的,当JVM中只有守护线程时,守护线程会自动销毁,JVM会退出。
调用线程组的setDaemon(true)可以把线程组设置为守护线程组,当守护线程组中没有任何活动线程时,守护线程组会自动销毁。
注意线程组的守护属性,不影响线程组中线程的守护属性,或者说守护线程组中的线程可以是非守护线程。
package com.wkcto.threadgroup;
/**
* 演示设置守护线程组
*/
public class Test05 {
public static void main(String[] args) throws InterruptedException {
//先定义线程组
ThreadGroup group = new ThreadGroup("group");
//设置线程组为守护线程组
group.setDaemon(true);
//向组中添加3个线程
for (int i = 0; i < 3; i++) {
new Thread(group, new Runnable() {
@Override
public void run() {
for (int j = 0; j < 20; j++) {
System.out.println(Thread.currentThread().getName() + " -- " + j);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
}
//main线程睡眠5秒
Thread.sleep(5000);
System.out.println("main...end....");
}
}