Java多线程之---ThreadGroup 管理Thread

当创建了好几个线程的时候,很多线程的工作任务是类似或者一致的,这样我们就可以使用ThreadGroup来管理他

们,ThreadGroup可以随时的获取在他里面的线程的运行状态,信息,或者一条命令关闭掉这个group里面的所有线

程,非常的简单实用,下面我们用一个例子来说明一下如何使用。

 

package com.nerve.core.test;


import java.util.Date;
import java.util.Random;
import java.util.concurrent.TimeUnit;

//http://blog.csdn.net/a352193394/article/details/39323427#
public class SearchTask implements Runnable {


	public SearchTask(Result result) {
		this.result = result;
	}

	private Result result;

	@Override
	public void run() {
		String name = Thread.currentThread().getName();
		System.out.println("Thread Start " + name);
		try {
			doTask();
			result.setName(name);
		} catch (InterruptedException e) {
			System.out.printf("Thread %s: Interrupted\n", name);
			return;
		}
		System.out.println("Thread end " + name);
	}

	private void doTask() throws InterruptedException {
		Random random = new Random((new Date()).getTime());
		int value = (int) (random.nextDouble() * 100);
		System.out.printf("Thread %s: %d\n", Thread.currentThread().getName(),value);
		
		TimeUnit.SECONDS.sleep(value);
	}

	public static void main(String[] args) {
		//创建5个线程,并入group里面进行管理
		ThreadGroup threadGroup = new ThreadGroup("Searcher");
		
		Result result = new Result();
		
		SearchTask searchTask = new SearchTask(result);
		for (int i = 0; i < 5; i++) {
			
			Thread thred = new Thread(threadGroup, searchTask);
			thred.start();
			
			try {
				
				TimeUnit.SECONDS.sleep(1);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		//通过这种方法可以看group里面的所有信息
		System.out.printf("Number of Threads: %d\n", threadGroup.activeCount());
		System.out.printf("Information about the Thread Group\n");
		threadGroup.list();

		//这样可以复制group里面的thread信息
		Thread[] threads = new Thread[threadGroup.activeCount()];
		threadGroup.enumerate(threads);
		
		for (int i = 0; i < threadGroup.activeCount(); i++) {
			System.out.printf("Thread %s: %s\n", threads[i].getName(),threads[i].getState());
		}
		
		//等待所有线程退出
		waitFinish(threadGroup);
		//将group里面的所有线程都给interpet
		threadGroup.interrupt();
	}

	private static void waitFinish(ThreadGroup threadGroup) {
		while (threadGroup.activeCount() > 9) {
			try {
				TimeUnit.SECONDS.sleep(1);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}

}
package com.nerve.core.test;

public class Result {

	private String name;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	
}


转自:http://blog.csdn.net/a352193394/article/details/39323427#


 
import java.util.Date;
import java.util.Random;
import java.util.concurrent.TimeUnit;
 
//http://blog.csdn.net/a352193394/article/details/39323427#
public class SearchTask implements Runnable {
 
	public SearchTask(Result result) {
		this.result = result;
	}
 
	private Result result;
 
	public void run() {
		String name = Thread.currentThread().getName();
		System.out.println("Thread Start " + name);
		try {
			doTask();
			result.setName(name);
		} catch (InterruptedException e) {
			System.out.printf("Thread %s: Interrupted\n", name);
			return;
		}
		System.out.println("Thread end " + name);
		
	}
 
	private void doTask() throws InterruptedException {
		Random random = new Random((new Date()).getTime());
		int value = (int) (random.nextDouble() * 10)+2;
		System.out.printf("doTask Thread %s: %d s\n", Thread.currentThread().getName(),value);
		
		TimeUnit.SECONDS.sleep(value);
	}
 
	public static void main(String[] args) {
		//创建5个线程,并入group里面进行管理
		ThreadGroup threadGroup = new ThreadGroup("Searcher");
		
		Result result = new Result();
		
		SearchTask searchTask = new SearchTask(result);
		for (int i = 0; i < 5; i++) {
			
			Thread thred = new Thread(threadGroup, searchTask);
			thred.setName("task"+i);
			thred.start();
			
			try {
				
				TimeUnit.SECONDS.sleep(1);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		System.out.println();
		//通过这种方法可以看group里面的所有信息
		System.out.printf("Number of Threads: %d\n", threadGroup.activeCount());
		System.out.printf("Information about the Thread Group\n");
		//打印线程组信息
		threadGroup.list();
 
		//这样可以复制group里面的thread信息
		Thread[] threads = new Thread[threadGroup.activeCount()];
		threadGroup.enumerate(threads);
		
		for (int i = 0; i < threadGroup.activeCount(); i++) {
			System.out.printf("enumerate Thread %s: %s\n", threads[i].getName(),threads[i].getState());
		}
		
		//等待所有线程退出
		waitFinish(threadGroup);
		//将group里面的所有线程都给interpet
		threadGroup.interrupt();
		
		System.out.println("all finish!");
	}
 
	private static void waitFinish(ThreadGroup threadGroup) {
		
		while (threadGroup.activeCount() > 0) {
			//线程组中有未完成的就等待
			try {
				TimeUnit.SECONDS.sleep(1);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
}

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值