[JAVA100 example]063, line Cheng Group

Source: Internet
Author: User
Tags sleep thread class

/**


* <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);


   }


  }


  }


}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.