Java Multithreading--Implement Runnable interface mode

Source: Internet
Author: User

Because Java classes can only inherit one class to implement multiple interfaces, it is generally not recommended to use the inherited thread class for multi-threading, which is a simple multithreaded code that implements the Runnable interface approach.
 Packagetext;/*** Multithreading *@authorAdmin **/ Public classThreads { Public Static voidMain (string[] args) {thread_1 T1=Newthread_1 (); Thread Thread1=NewThread (t1);                Thread1.start (); Thread_2 T2=Newthread_2 (); Thread thread2=NewThread (T2);            Thread2.start (); }}    classThread_1Implementsrunnable{ Public voidrun () {Try {            //thread paused for one secondThread.Sleep (1000);//This method is simply added to see the threading effect more directlySystem.out.println ("Execution Method 1"); } Catch(interruptedexception e) {e.printstacktrace (); }                }}classThread_2Implementsrunnable{ Public voidrun () {Try{Thread.Sleep (1000); System.out.println ("Execution Method 2"); } Catch(interruptedexception e) {e.printstacktrace (); }            }    }    

Java Multithreading--Implement Runnable interface mode

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.