Multithreading _yield () and sleep () methods comparison

Source: Internet
Author: User

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

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.