Initial Learning Multithreading 3

Source: Internet
Author: User

Java Source code display:

Bunny Thread: Rabbitthread.java

 package   thread;  /**   * Rabbit Thread *   @author   Superdrew  */ public  class  rabbitrunnable implements   runnable{ public  void   run () {  while  (true  ) The rabbit is ahead .... Come on!!!! " + Thread.CurrentThread (). GetName ()  + "+        Thread.CurrentThread (). getpriority ()); }    }}

Turtle Thread: Tortoisethread.java

 Packagethread;ImportJava.util.Scanner; Public classTortoisethreadextendsthread{PrivateString name;  PublicTortoisethread () {}//How to construct the thread of a parent class by passing a value     PublicTortoisethread (String name) {Super(name); }         Public voidrun () { while(true) {Scanner sc=NewScanner (system.in); System.out.println ("The turtle is leading the ... Come on...... "+getname () +" "+getpriority ()); }    }}

Test thread: Testthread_main.java

 Packagethread;/*** Function: Tortoise and Rabbit race * Use thread *@authorSuperdrew * * Flexible implementation of multithreading * How many threads are there at this time?                                3 main turtle Thread Bunny thread * How to give the thread a name implements the interface Thread.setname * inherits the thread the first setName () parent class *                 The second way to pass values to the parent class by constructing a method Thread * * Common method: Run () Start () Thread.CurrentThread () getName () * SetName () getpriority () setpriority () * * All the people coming to the race, using threads to write 100 threads? * Create a new player thread * 1. Player name * 2. Thread name **/ Public classTestthread { Public Static voidMain (string[] args) {//Rabbit ThreadRabbitrunnable rr =Newrabbitrunnable (); Thread TR=NewThread (RR); Tr.setname ("Rabbit Thread");//set a name for a threadTr.start ();//Start the rabbit thread//Turtle ThreadTortoisethread TT =NewTortoisethread ("Turtle thread"); Tt.setname ("Turtle thread");//set a name for a threadTt.start (); }}

Initial Learning Multithreading 3

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.