JAVA two ways to implement multithreading (inherit thread and implement Runnable interface)

Source: Internet
Author: User

1  Packageh14;2 3  Public classDemo {4 5     /**6      * @paramargs7      */8      Public Static voidMain (string[] args) {9         //TODO auto-generated Method StubTenThread cat =NewCat (); One Cat.start (); ARunnable dog =NewDog (); -Thread T =NewThread (dog); - T.start (); the     } -  - } -  + classCatextendsthread{ -  + @Override A      Public voidrun () { at         //TODO auto-generated Method Stub -         Super. Run (); -         inti = 0; -          while(true){ -             Try { -i++; inThread.Sleep (1000); -}Catch(interruptedexception e) { to                 //TODO auto-generated Catch block + e.printstacktrace (); -             } the System.out.println (i); *         } $     }Panax Notoginseng      - } the  + classDogImplementsrunnable{ A  the @Override +      Public voidrun () { -         //TODO auto-generated Method Stub $System.out.print ("Hello"); $          -     } -      the}

JAVA Two methods implement multithreading (Inherit thread and implement Runnable interface)

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.