Two methods of creating threads, inheriting thread, inheriting runnable

Source: Internet
Author: User

Inherit thread

1  Packagethread;2 //Inherit thread3  Public classTestthread {4      Public Static voidMain (string[] args) {5         //the Run () method is called by the CPU;6MyThread thread=NewMyThread ();7 Thread.Start ();8         //Main Thread9String Name=thread.currentthread (). GetName ();//get the name of the threadTen          for(inti=0;i<=1000;i++){ OneSystem.out.println (name+i); A         } -     }              -     Static classMyThreadextendsthread{ the  - @Override -          Public voidrun () { -String Name=thread.currentthread (). GetName ();//get the name of the thread +             Super. Run (); -              for(inti=1;i<=10;i++){ +System.out.println (name+i); A             } at         } -      -      -     } -}

Inherit runnable

1  Packagethread;2 //Inherit runnable3  Public classtestrunnable {4      Public Static voidMain (string[] args) {5         //Split Thread6Myrunnable rannable=Newmyrunnable ();7Thread thread=NewThread (rannable);8 Thread.Start ();9         //Main ThreadTenString Name=thread.currentthread (). GetName ();//get the name of the thread One          for(inti=1;i<=10;i++){ ASystem.out.println (name+i); -         } -     } the     Static classMyrunnableImplementsrunnable{ -  - @Override -          Public voidrun () { +String Name=thread.currentthread (). GetName ();//get the name of the thread -              for(inti=1;i<=10;i++){ +System.out.println (name+i); A             } at              -         } -          -     } -}

Two methods of creating threads, inheriting thread, inheriting runnable

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.