Android (Java) Learning Note 73: Overview and use of thread groups

Source: Internet
Author: User

Java uses Threadgroup to represent thread groups , which can classify a batch of threads, and Java allows programs to control thread groups directly.

(1) By default, all threads belong to the main thread group . Public final Threadgroup Getthreadgroup ()

(2) We can also set thread (threadgroup Group, Runnable Target, String name) for threads

Below I follow the code to achieve:

1  Packagecn.itcast_06;2 3  Public classMyrunnableImplementsRunnable {4 5 @Override6      Public voidrun () {7          for(intx = 0; x < 100; X + +) {8System.out.println (Thread.CurrentThread (). GetName () + ":" +x);9         }Ten     } One  A}

1  Packagecn.itcast_06;2 3 /*4 * Thread Group: Combine multiple threads together. 5 * It can classify a batch of threads, and Java allows the program to control the thread group directly. 6  */7  Public classThreadgroupdemo {8      Public Static voidMain (string[] args) { -Myrunnable my =Newmyrunnable (); theThread T1 =NewThread (My, "Brigitte"); -Thread t2 =NewThread (My, "Elina"); -         //I don't know what they belong to that thread group and I want to know what to do -         //methods inside the thread class: Public final Threadgroup Getthreadgroup () +Threadgroup TG1 =T1.getthreadgroup (); -Threadgroup TG2 =T2.getthreadgroup (); +         //method inside the thread group: public final String getName () AString name1 =tg1.getname (); atString name2 =tg2.getname (); - System.out.println (name1); - System.out.println (name2); -         //we know through the results: threads belong to the main thread group by default -         //with the test below, you should be able to see that all threads belong to the same group in the default case -System.out.println (Thread.CurrentThread (). Getthreadgroup (). GetName ());
T1.start ();
T1.start ();
}
}

The output is:

Of course in real life we want to customize the thread group, all next we explain the custom thread group :

Here we have to say that the logic of custom thread groups is as follows:

(1) Create a group of threads

(2) When creating other threads, designate the group of other threads as our own new thread group

1  Packagecn.itcast_06;2 3  Public classMyrunnableImplementsRunnable {4 5 @Override6      Public voidrun () {7          for(intx = 0; x < 100; X + +) {8System.out.println (Thread.CurrentThread (). GetName () + ":" +x);9         }Ten     } One  A}
1  Packagecn.itcast_06;2 3 /*4 * Thread Group: Combine multiple threads together. 5 * It can classify a batch of threads, and Java allows the program to control the thread group directly. 6  */7  Public classThreadgroupdemo {8      Public Static voidMain (string[] args) {9         //method1 ();Ten  One         //How do we modify the group where the thread resides? A         //Create a group of threads -         //when creating another thread, designate the other thread's group as our own new thread group - method2 (); the  -         //T1.start (); -         //T2.start (); -     } +  -     Private Static voidmethod2 () { +         //Threadgroup (String name) AThreadgroup TG =NewThreadgroup ("This is a new group"); at  -Myrunnable my =Newmyrunnable (); -         //Thread (threadgroup Group, Runnable Target, String name), specifying T1 and T2 for the thread group TG -Thread T1 =NewThread (TG, My, "Brigitte")); -Thread t2 =NewThread (TG, My, "Elina")); -         in System.out.println (T1.getthreadgroup (). GetName ()); - System.out.println (T2.getthreadgroup (). GetName ()); to          +         //set up a background thread through the group name, indicating that the group's threads are background threads, where you can set up the thread group so that all the threads in the thread group  -Tg.setdaemon (true); the     } *  $     Private Static voidmethod1 () {Panax NotoginsengMyrunnable my =Newmyrunnable (); -Thread T1 =NewThread (My, "Brigitte"); theThread t2 =NewThread (My, "Elina"); +         //I don't know what they belong to that thread group and I want to know what to do A         //methods inside the thread class: Public final Threadgroup Getthreadgroup () theThreadgroup TG1 =T1.getthreadgroup (); +Threadgroup TG2 =T2.getthreadgroup (); -         //method inside the thread group: public final String getName () $String name1 =tg1.getname (); $String name2 =tg2.getname (); - System.out.println (name1); - System.out.println (name2); the         //we know through the results: threads belong to the main thread group by default -         //with the test below, you should be able to see that all threads belong to the same group in the default caseWuyi System.out.println (Thread.CurrentThread (). Getthreadgroup (). GetName ()); the     } -}

Android (Java) Learning Note 73: Overview and use of 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.