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