Multithreaded thread groups

Source: Internet
Author: User

1  PackageOrg.zln.thread;2 3 Importjava.util.Date;4 5 /**6 * Created by Sherry on 000024/6/24 22:30.7  */8  Public classTestthreadgroup {9      Public Static voidMain (string[] args)throwsinterruptedexception {TenThreadgroup group1 =NewThreadgroup ("Group1"); One         /*group2 from belonging to Group1 Group1 is the parent group2 is the child*/ AThreadgroup group2 =NewThreadgroup (group1, "group2"); -         /*associating threads with thread groups*/ -Thread T1 =NewThread (Group1,NewTestthread ("AAA")); theThread t2 =NewThread (Group2,NewTestthread ("BBB"))); -Thread t3 =NewThread (Group2,NewTestthread ("CCC"))); - T1.start (); - T2.start (); + T3.start (); -System.out.println ("Number of thread Group 1 threads:" +Group1.activecount ()); +System.out.println ("Number of thread Group 2 threads:" +Group2.activecount ()); ASystem.out.println ("Number of thread Group 1 thread groups:" +Group1.activegroupcount ()); at  -         /*Stop All tasks after 10 seconds*/ -          for(inti = 0; I < 10; i++) { -Thread.Sleep (1000); -System.out.println (NewDate ()); -         } inGroup1.stop ();//This is just to demonstrate that stop is not safe. Do not use in actual development -     } to}
1  PackageOrg.zln.thread;2 3 Importjava.util.Date;4 5 /**6 * Created by Coolkid on 2015/6/21 0021.7  */8  Public classTestthreadextendsthread{9     Private intTime//Sleep TimeTen     PrivateString user;//Call User One  A      PublicTestthread (intTime , String user) { -          This. Time =Time ; -          This. user =user; the     } -  - @Override -      Public voidrun () { +          while(true){ -             Try { +System.out.println (Thread.CurrentThread (). GetName () + "\ T" +user+ "Rest" +time+ "ms-" +NewDate ()); A Thread.Sleep (time); at}Catch(interruptedexception e) { - e.printstacktrace (); -             } -         } -     } -  in      Public Static voidMain (string[] args) { -Thread Thread1 =NewTestthread ("Jack")); toThread thread2 =NewTestthread, "Mike"); + thread2.setpriority (thread.max_priority); - Thread1.start (); the Thread2.start (); *     } $}

The advantage of using a thread group is the ability to bulk manipulate threads within a thread group, as well as to include relationships between thread groups, or parent-child relationships

Multithreaded thread groups

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.