State transitions, thread groups for Java 22-18 Multi-thread threads

Source: Internet
Author: User

State transition plots for threads:. Blog temporarily can't upload pictures

Thread groups for threads :

Thread groups: combine multiple threads together.
It can classify a batch of threads, and Java allows the program to control the thread group directly.

First, create a Runnable implementation class

 Public class Implements Runnable {    publicvoid  run () {        for (int x = 0; x < 10; x + +) {            System.out.println (x);}}    }

Let's take a look at the default state, in which thread groups the thread will be saved

1     Private Static voidmethod1 () {2         //thread in the default thread array3         4         //Creating Objects5Myrunnable Mr =Newmyrunnable ();6         7         //To create a thread object8Thread T1 =NewThread (MR, "ah three");9Thread t2 =NewThread (MR, "Ah Si");Ten          One         // If you do not add threads to a custom thread array, they are added by default to a thread array given by the system  A         //Let's see what this default thread array is. -         //Public  Final Threadgroup getthreadgroup () returns the thread group to which the thread belongs. if the thread has terminated (stops running), the method returns NULL.  -Threadgroup TG1 =T1.getthreadgroup (); theThreadgroup TG2 =T2.getthreadgroup (); -          -         //View the name of this array - System.out.println (Tg1.getname ()); +         //we know through the results: threads belong to the main thread group by default  -          +         //chained method to view the name of this array A         //with the test below, you should be able to see that all threads belong to the same group in the default case at System.out.println (Thread.CurrentThread (). Getthreadgroup (). GetName ()); -  -}

As can be known from above, threads belong to the main thread group by default

We then have the thread added to the custom thread group, and the threads in the thread group are uniformly controlled

1     Private Static voidmethod2 () {2         //threads are added to the custom thread array3         4         //Creating Objects5Myrunnable Mr =Newmyrunnable ();6         //creating an array of threads7Threadgroup TG =NewThreadgroup ("Custom Thread Array");8         9         //Creating ThreadsTen         // Thread (threadgroup Group, Runnable Target, String name)  One         //assigns a new thread object to use target as its running object, the specified name as its name, and as a member of the group of threads referenced by group.  AThread T1 =NewThread (TG,MR, "Ah three"); -Thread t2 =NewThread (TG,MR, "Ah si"); -          the         //View the name of the thread array where T1 and T2 are now located - System.out.println (T1.getthreadgroup (). GetName ()); -          -         //T1, T2 can be operated uniformly +         //set all the threads in the TG array to be daemon threads -Tg.setdaemon (true); +          A}

State transitions, thread groups for Java 22-18 Multi-thread threads

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.