/**
* <p>title: 线程组群</p>
* <p>description: 通过线程组管理下面的多个线程。</p>
* <p>copyright: copyright (c) 2003</p>
* <p>filename: mythreadgroup.java</p>
* @version 1.0
*/
public class mythreadgroup extends thread {
public static int flag=1;
threadgroup tga;
threadgroup tgb;
/**
*<br>方法说明:主方法
*<br>输入参数:
*<br>返回类型:
*/
public static void main(string[] args){
mythreadgroup dt = new mythreadgroup();
//声明线程组a
dt.tga = new threadgroup("a");
//声明线程组b
dt.tgb = new threadgroup("b");
for(int i=1;i<3;i++)
new thread1(dt.tga,i*1000,"one"+i);
for(int i=1;i<3;i++)
new thread1(dt.tgb,1000,"two"+i);
//启动本线程和所有线程组
dt.start();
}
/**
*<br>方法说明:覆盖run方法,控制线程组
*<br>输入参数:
*<br>返回类型:
*/
public void run(){
try{
this.sleep(5000);
this.tgb.checkaccess();
//停止线程组b,
this.tgb.stop();
system.out.println("**************tgb stop!***********************");
this.sleep(1000);
//检查线程组a是否可以更改
this.tga.checkaccess();
//停止线程组a
this.tga.stop();
system.out.println("**************tga stop!***********************");
}catch(securityexception es){
system.out.println("**"+es);
}catch(exception e){
system.out.println("::"+e);
}
}
}
/**
* <p>title: 线程类</p>
* <p>description: 通过构造器的参数,实现不同的线程</p>
* <p>copyright: copyright (c) 2003</p>
* <p>filename: thread1.java</p>
* @author 杜江
* @version 1.0
*/
class thread1 extends thread {
int pausetime;
string name;
public thread1(threadgroup g,int x, string n) {
super(g,n);
pausetime = x;
name = n;
start();
}
/**
*<br>方法说明:必须覆盖的方法。
*<br>输入参数:
*<br>返回类型:
*/
public void run ()
{
while(true) {
try {
system.out.print(name+"::::");
this.getthreadgroup().list();//获取线程组信息
thread.sleep(pausetime);
} catch(exception e) {
system.out.println(e);
}
}
}
}
闽公网安备 35060202000074号