Learn to summarize Java threads

Source: Internet
Author: User

1  PackageCom.hanqi.xc;2 3  Public classTest1 {4 5      Public Static voidMain (string[] args) {6 7         //Threading Test8          for(inti = 0; I < 10; i++) {9System.out.println ("i =" +i);Ten  One             //controlling threads through thread classes A             Try { -                 //let the main thread hibernate for 100 milliseconds -Thread.Sleep (100); the  -}Catch(interruptedexception e) { -                 //TODO Auto-generated catch block - e.printstacktrace (); +             } -  +         } A  atTest2 t2 =NewTest2 (); -  -         //Single Thread -         //t2.test (); -  -         //start a new thread in  - T2.start (); to  +Test2 t3 =NewTest2 (); -          the T3.start (); *          $Test2 T4 =NewTest2 ();Panax Notoginseng          - T4.start (); the          +         //to start the interface mode of the sub-threading A          theThread th =NewThread (NewTest3 (), "Interface thread 4"); +          - Th.start (); $     } $  -      -      the      -     Wuyi      the      -      Wu      -}

 PackageCom.hanqi.xc;//support multithreading in an inherited way Public classTest2extendsThread {//overriding the Run method@Override Public voidrun () {//statements that need to be executed concurrentlytest (); }                //test Method     Public voidTest () { for(inti = 0; I < 10; i++) {System.out.println ("I =" +i); //controlling threads through thread classes            Try             {                //let the main thread hibernate for 100 millisecondsThread.Sleep (100); } Catch(interruptedexception e) {//TODO Auto-generated catch blockE.printstacktrace (); }        }            }
 PackageCom.hanqi.xc; Public classTest3ImplementsRunnable {@Override Public voidrun () {//code that needs to be executed in a thread         for(inti = 0; I < 10; i++) {System.out.println ("I =" +i); //controlling threads through thread classes            Try             {                //let the main thread hibernate for 100 millisecondsThread.Sleep (100); } Catch(interruptedexception e) {//TODO Auto-generated catch blockE.printstacktrace (); }        }    }}

Learn to summarize Java threads

Related Article

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.