9.多线程学习--线程组

来源:互联网 发布:电脑时间校准软件 编辑:程序博客网 时间:2024/06/06 04:45
package com.jackson.deng.concurrent.chapter1.nine;import java.util.Date;import java.util.Random;import java.util.concurrent.TimeUnit;/** * 线程组 * function desc : 有 10 个随机时间休眠的线程 (例如,模拟搜索),然后当其中一个完成,就中断其余的 * summary : ThreadGroup 类储存线程对象和其他有关联的 ThreadGroup 对象,所以它可以访问他们的所有信息 (例如,状态) 和全部成员的操作表现 (例如,中断)。 * @author jackson * */public class ThreadGroupTest {public class SearchTask implements Runnable {private Result result;public SearchTask(Result result) {this.result = result;}@Overridepublic void run() {String name = Thread.currentThread().getName();System.out.println("Thread " + name + " : start");try {doTask();result.setName(name);} catch (InterruptedException e) {System.out.println("Thread " + name + " : interrupted");return;}System.out.println("Thread " + name + " : finished");}private void doTask() throws InterruptedException {Random r = new Random(new Date().getTime());int value = (int) (r.nextDouble() * 100);System.out.println("Thread " + Thread.currentThread().getName() + " waiting : " + value);TimeUnit.SECONDS.sleep(value);}}public static void main(String[] args) {//新建一个线程组searchGroupThreadGroup group = new ThreadGroup("searchGroup");Result result = new Result();ThreadGroupTest test = new ThreadGroupTest();SearchTask task = test.new SearchTask(result);for (int i = 0; i < 5; i++) {Thread thread = new Thread(group, task);thread.start();try {TimeUnit.SECONDS.sleep(1);} catch (InterruptedException e) {e.printStackTrace();}}System.out.println("Numnber of thread : " + group.activeCount());System.out.println("The information about thread : ");group.list();Thread[] threads = new Thread[group.activeCount()];group.enumerate(threads);for (int i = 0; i < group.activeCount(); i++) {System.out.println("Thread " + threads[i].getName() + " : " + threads[i].getState());}waitFinish(group);group.interrupt();//用interrupt() 方法中断组里的其他线程。}private static void waitFinish(ThreadGroup group) {while (group.activeCount() > 9) {//只有5个,直接跳出完成任务try {TimeUnit.SECONDS.sleep(1);} catch (InterruptedException e) {e.printStackTrace();}}}}class Result {private String name;public String getName() {return name;}public void setName(String name) {this.name = name;}}

运行结果:

Thread Thread-0 : start
Thread Thread-0 waiting : 23
Thread Thread-1 : start
Thread Thread-1 waiting : 14
Thread Thread-2 : start
Thread Thread-2 waiting : 41
Thread Thread-3 : start
Thread Thread-3 waiting : 31
Thread Thread-4 : start
Thread Thread-4 waiting : 11
Numnber of thread : 5
The information about thread : 
java.lang.ThreadGroup[name=searchGroup,maxpri=10]
    Thread[Thread-0,5,searchGroup]
    Thread[Thread-1,5,searchGroup]
    Thread[Thread-2,5,searchGroup]
    Thread[Thread-3,5,searchGroup]
    Thread[Thread-4,5,searchGroup]
Thread Thread-0 : TIMED_WAITING
Thread Thread-1 : TIMED_WAITING
Thread Thread-2 : TIMED_WAITING
Thread Thread-3 : TIMED_WAITING
Thread Thread-4 : TIMED_WAITING
Thread Thread-0 : interrupted
Thread Thread-1 : interrupted
Thread Thread-2 : interrupted
Thread Thread-3 : interrupted
Thread Thread-4 : interrupted

0 0