Thread--Inherit thread

Source: Internet
Author: User

First inherit the thread class, and then override the run () method of the thread class.

The object of the child class of the thread class calls the start () method, and then the virtual machine calls the thread's run () method.

When the program executes to the start () method, the thread starts, there are two execution paths at this time, one is the main method executes the main method, the other is the code in the thread path execution thread run (), and two paths are executed alternately (alternating execution refers to the Snatch Cup execution time, so each execution result is different)

classThreaddemoextendsthread{ Public voidrun ()//store thread to execute the code { for(inti = 0; I < 60; i++) {System.out.println ("Threads" +i); }    }   }classthreadtest{ Public Static voidMain (string[] args) {Threaddemo thread=NewThreaddemo (); Thread.Start ();//The thread starts and executes the thread's run () method, and the main path and thread are executed alternately                    for(inti = 0; I < 60; i++) {System.out.println ("Mian" +i); }    }}

Thread--Inherit thread

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.