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