如果想对一组线程进行统一操作,可以使用ThreadGroup类
范例: 模拟搜索文件,开启十个线程查询文件(使用sleep来模拟),当其中有一个线程找到之后,interrupt。
package com.rr.concurrent.chapter1.recipe10.test;
import java.util.Random;
import java.util.concurrent.TimeUnit;
/**
* Created by isaac_gu on 2016/5/11.
*/
public class SearchTask implements Runnable {
@Override
public void run() {
try {
int time = new Random().nextInt(10) + 15;
System.out.printf("%s 线程开始查找(%d秒)!\n", Thread.currentThread().getName(), time);
//查询time秒
TimeUnit.SECONDS.sleep(time);
System.out.printf("%s 线程找到文件!\n", Thread.currentThread().getName());
} catch (InterruptedException e) {
System.out.printf("%s 线程被打断!\n", Thread.currentThread().getName());
return;
}
}
}
测试:
package com.rr.concurrent.chapter1.recipe10.test;
import java.util.concurrent.TimeUnit;
/**
* Created by isaac_gu on 2016/5/11.
* 范例: 模拟搜索文件,开启十个线程查询文件(使用sleep来模拟),当其中有一个线程找到之后,interrupt
*/
public class Test {
public static void main(String[] args) {
//定义一个线程组
ThreadGroup threadGroup = new ThreadGroup("searcher");
SearchTask searchTask = new SearchTask();
//每秒创建一个查询线程
for (int i = 0, len = 10; i < len; i++) {
//把线程组加到线程中
new Thread(threadGroup, searchTask).start();
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//打印查询期间 threadGroup的信息
printDetail(threadGroup);
//等待查询结果 - 每秒查看一次运行情况
waitResult(threadGroup);
//打断
threadGroup.interrupt();
}
/**
* 打印查询期间 threadGroup的信息
*
* @param threadGroup
*/
private static void printDetail(ThreadGroup threadGroup) {
System.out.println("******************************** threadGroup detail ******************************");
System.out.println(" ------ threadGroup.list(): ------");
threadGroup.list();
System.out.println(" ------ 当前活动的线程数 ------");
System.out.println(threadGroup.activeCount());
System.out.println(" ------ 每个线程的状态 ------");
//创建一个线程数组
Thread[] threads = new Thread[threadGroup.activeCount()];
//把活动的线程加到线程数组中
threadGroup.enumerate(threads);
//遍历
for (Thread thread : threads) {
System.out.printf("%s 线程的状态: %s\n", thread.getName(), thread.getState());
}
System.out.println("******************************** threadGroup detail END ******************************");
}
/**
* 每秒查询一次结果
*
* @param threadGroup
*/
private static void waitResult(ThreadGroup threadGroup) {
while (threadGroup.activeCount() > 9) {
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
运行结果:
Thread-0 线程开始查找(15秒)!
Thread-1 线程开始查找(22秒)!
Thread-2 线程开始查找(19秒)!
Thread-3 线程开始查找(24秒)!
Thread-4 线程开始查找(15秒)!
Thread-5 线程开始查找(20秒)!
Thread-6 线程开始查找(19秒)!
Thread-7 线程开始查找(22秒)!
Thread-8 线程开始查找(17秒)!
Thread-9 线程开始查找(20秒)!
******************************** threadGroup detail ******************************
------ threadGroup.list(): ------
java.lang.ThreadGroup[name=searcher,maxpri=10]
Thread[Thread-0,5,searcher]
Thread[Thread-1,5,searcher]
Thread[Thread-2,5,searcher]
Thread[Thread-3,5,searcher]
Thread[Thread-4,5,searcher]
Thread[Thread-5,5,searcher]
Thread[Thread-6,5,searcher]
Thread[Thread-7,5,searcher]
Thread[Thread-8,5,searcher]
Thread[Thread-9,5,searcher]
------ 当前活动的线程数 ------
10
------ 每个线程的状态 ------
Thread-0 线程的状态: TIMED_WAITING
Thread-1 线程的状态: TIMED_WAITING
Thread-2 线程的状态: TIMED_WAITING
Thread-3 线程的状态: TIMED_WAITING
Thread-4 线程的状态: TIMED_WAITING
Thread-5 线程的状态: TIMED_WAITING
Thread-6 线程的状态: TIMED_WAITING
Thread-7 线程的状态: TIMED_WAITING
Thread-8 线程的状态: TIMED_WAITING
Thread-9 线程的状态: TIMED_WAITING
******************************** threadGroup detail END ******************************
Thread-0 线程找到文件!
Thread-4 线程被打断!
Thread-7 线程被打断!
Thread-8 线程被打断!
Thread-3 线程被打断!
Thread-2 线程被打断!
Thread-1 线程被打断!
Thread-9 线程被打断!
Thread-5 线程被打断!
Thread-6 线程被打断!