Java Multithreading thread and runnable

Source: Internet
Author: User

First, Runnable

  Runnable as an interface that contains a run method

1  Public classMyrunnableImplementsrunnable{2@Override//indication: Indicates an overriding method3      Public voidRun () {//implementing the interface Run method4SYSTEM.OUT.PRINTLN ("Thread 0.0\t to implement runnable" +Thread.CurrentThread (). GetName ());5         6     }7     8      Public Static voidMain (string[] args) {9         //Create a Runnable objectTenMyrunnable Run =Newmyrunnable (); One         //Create a thread object with the object run as a parameter AThread THR1 =NewThread (Run, "thread-1"); -Thread THR2 =NewThread (Run, "Thread-2"); -Thread THR3 =NewThread (Run, "Thread-3"); theThread THR4 =NewThread (Run, "Thread-4"); -Thread THR5 =NewThread (Run, "Thread-5"); -         //Start Thread - Thr1.start (); + Thr2.start (); - Thr3.start (); + Thr4.start (); A Thr5.start (); at     } -  -  -}

Run the result as

Second, Thread

  Thread is a class that implements the Runnable

1  Public classThreadsextendsthread{2     //Construction Method3 String name;4 Threads (String name) {5          This. Name =name;6     }7     //overriding the Run method8      Public voidrun () {9SYSTEM.OUT.PRINTLN ("Thread 0.0\t to implement runnable" +Thread.CurrentThread (). GetName ());Ten     } One  A      Public Static voidMain (string[] args) { -         //Create a Treads object -Threads THR1 =NewThreads ("Thread-1"); theThreads THR2 =NewThreads ("Thread-2"); -Threads THR3 =NewThreads ("Thread-3"); -Threads THR4 =NewThreads ("Thread-4"); -Threads THR5 =NewThreads ("Thread-5"); +         //Start Thread - Thr1.start (); + Thr2.start (); A Thr3.start (); at Thr4.start (); - Thr5.start (); -  -     } -  -}

Run results

Java Multithreading thread and 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.