您的位置:首页 > 编程语言 > Java开发

Demo20 :通过线程组管理多线程(通过构造器的参数实现不同的线程)

2018-04-05 08:24 288 查看
package demo20;

/**
* <p>Title: 线程组群</p>
* <p>Description: 通过线程组管理下面的多个线程。</p>
*/
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>
*/
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);
}
}
}
}


运行结果:





内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java 线程组群 Thread
相关文章推荐