/**
* <p>title: Thread group </p>
* <p>description: Manages multiple threads below through a thread group. </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> Method Description: Main method
*<br> input Parameters:
*<br> return type:
*/
public static void Main (string[] args) {
Mythreadgroup dt = new Mythreadgroup ();
//Declaration thread Group A
dt.tga = new Threadgroup ("A");
//Declaration thread Group 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);
//Start this thread and all thread groups
Dt.start ();
}
/**
*<br> Method Description: Override run method, control thread group
*<br> input Parameters:
*<br> return type:
*/
public void Run () {
try{
This.sleep (5000);
this.tgB.checkAccess ();
//Stop thread Group B,
This.tgB.stop ();
System.out.println ("**************tgb stop!***********************");
this.sleep (1000);
//Check if thread Group A can change
this.tgA.checkAccess ();
//Stop thread Group 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: Thread class </p>
* <p>description: Implements different threads </p>
via constructor parameters
* <p>copyright:copyright (c) 2003</p>
* <p>Filename:thread1.java</p>
* @author Dujiang
* @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> Method Description: The method that must be overridden.
*<br> input Parameters:
*<br> return type:
*/
public void Run ()
{
while (true) {
try {
System.out.print (name+ ":::");
this.getthreadgroup (). List ().//Get thread group information
Thread.Sleep (pausetime);
} catch (Exception e) {
System.out.println (e);
}
}
}
}