Examples of multithreading show:
Rabbitthread.java Threads:
1 Packagethread;2 3 /**4 * Rabbit's thread5 * @authorSuperdrew6 */7 Public classRabbitrunnableImplementsrunnable{8 9 Public voidrun () {Ten while(true){ One ASystem.out.println ("Rabbit lead ....") Come on!!!! " + -Thread.CurrentThread (). GetName () + "" +Thread.CurrentThread (). getpriority ()); - the //Thread.yield (); - } - } - +}
Rabbitthread.java
Tortoisethread.java Threads:
1 Packagethread;2 3 Public classTortoisethreadextendsthread{4 //Thread property Name:5 PrivateString name;6 //Non-parametric construction method7 PublicTortoisethread () {}8 //Parametric construction Method9 PublicTortoisethread (String name) {Ten Super(name); One } A - Public voidrun () { - while(true){ theSystem.out.println ("Turtle leading ... Come on...... "+getname () +" "+getpriority ()); - Thread.yield (); - } - } +}
Tortoisethread.java
Testthread Threads:
1 Packagethread;2 /**3 * Function: Turtle and Rabbit race4 * Using Threads5 * yield ();6 * @authorSuperdrew7 * Role: Pauses the currently executing thread object and executes other threads8 * Call yield () the current thread enters a ready state and can compete with other ready-state threads for CPU9 * If other threads have high priority, then other threads have a greater chance of getting the CPU.Ten * One * yield () and sleep () difference A * 1.sleep () is paused into blocking state, only sleep time is ready to go - * Yield () is directly into the ready state, it is possible to get into the ready state to obtain the CPU and execute - * 2.yield () No abnormal sleep () has the * - * - */ - Public classTestThread1 { + Public Static voidMain (string[] args) { -Rabbitrunnable rr =Newrabbitrunnable (); +Thread TR =NewThread (RR); ATr.setname ("Rabbit Thread"); at Tr.start (); - -Tortoisethread TT =NewTortoisethread ("Turtle thread"); - Tt.start (); - } -}
Testthread.java
Multithreading _yield () and sleep () methods comparison