[JAVA100例]063、线程群组

王朝java/jsp·作者佚名  2008-05-31
宽屏版  字体: |||超大  

/**

* <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>方法说明:必须覆盖的方法。

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
© 2005- 王朝网络 版权所有