Review the use of sleep in Java multi-threading

Source: Internet
Author: User

The first is the problem that the classmate said, the main course first run, the new thread runs the problem, The result is the new thread is can and the main course pk, is a very basic problem, the feeling should be a computer problem.

Here is the test code:

1  packagetest;2 /**3  * @authorASCETICJ4 * @date April 20, 2017 pm 10:25:145  * @version1.06 * @TODO Test after opening a new thread in the main thread, the main thread and the new thread execution order, the results show that the new thread and the main thread can be PK7  */8  public classTestImplementsRunnable9 {Ten      public Static voidmain (string[] Args) one     { aTest test =NewTest (); -Thread thread =NewThread (test); - Thread.Start ();  the          for(inti=0; i<10; i++){ -System.out.println (thread.currentthread (). getName () + "hehe hey" +i); -         } -     } +      - @Override +      public voidRun () a     { at          for(inti=0; i<10; i++){ -System.out.println (thread.currentthread (). getName () + "hahaha" +i); -         } -     } -  -}

Test: the main thread and the new thread are executed alternately

The second is to see the sleep method does not release the lock problem

Test code:

1  packagetest;2 /**3  * @authorASCETICJ4 * @date April 20, 2017 pm 10:25:145  * @version1.06 * @TODO Test after calling the sleep () method in a synchronous code block, the thread does not release the lock7  */8  public classTest1ImplementsRunnable9 {Ten     PrivateString lock = "lock"; one     Private intCount = 0; a      public Static voidmain (string[] Args) -     { -Test1 test =NewTest1 (); theThread thread =NewThread (test); -Thread Thread1 =NewThread (test); - Thread.Start (); - Thread1.start (); +          -          for(inti=0; i<10; i++){ +System.out.println (thread.currentthread (). getName () + "-pk victory--is hehe hey! "+i); a         } at     } -      - @Override -      public voidRun () -     { -System.out.println (thread.currentthread (). getName () + "pk victory!" "); in          while(count<10){ -System.out.println (thread.currentthread (). getName () + "start to grab the lock!" "); to             synchronized(lock) +             { -System.out.println (thread.currentthread (). getName () + "grab The lock victory, are you hey!" "+ (count++)); the                 Try *                 { $Thread.Sleep (1000);Panax Notoginseng}Catch(interruptedexception E) -                 { the e.printstacktrace (); +                 } aSystem.out.println (thread.currentthread (). getName () + "hey hehe end, prepare for the next wave!" "+ (count++)); the             } +              -         } $     } $  -}

Test:

The third question is why you should use sleep in a synchronization block, and why a thread executes all without Sleep.

Test code, just a comment on sleep a no comment

1  packagetest;2 /**3  * @authorASCETICJ4 * @date April 20, 2017 pm 10:25:145  * @version1.06 * @TODO test calls Sleep () in a synchronous code block7  */8  public classTest3ImplementsRunnable9 {Ten     PrivateString lock = "lock"; one     Private intCount = 0; a      public Static voidmain (string[] Args) -     { -TEST3 test =NewTest3 (); theThread thread =NewThread (test); -Thread Thread1 =NewThread (test); -Thread thread2 =NewThread (test); - Thread.Start (); + Thread1.start (); - Thread2.start (); +          for(inti=0; i<10; i++){ aSystem.out.println (thread.currentthread (). getName () + "-pk victory--is hehe hey! "+i); at         } -     } -      - @Override -      public voidRun () -     { inSystem.out.println (thread.currentthread (). getName () + "pk victory!" "); -          while(count<10){ toSystem.out.println (thread.currentthread (). getName () + "start to grab the lock!" "); +             synchronized(lock) -             { theSystem.out.println (thread.currentthread (). getName () + "grab The lock victory, are you hey!" "+ (count++)); * //Try $ //                {Panax Notoginseng //Thread.Sleep (+); - //} catch (interruptedexception E) the //                { + //e.printstacktrace (); a //                } theSystem.out.println (thread.currentthread (). getName () + "hey hehe end, prepare for the next wave!" "+ (count++)); +             } -              $         } $     } -  -}

Test:

No sleep:

Sleep

finally, attach a thread boot order

Review the use of sleep in Java multi-threading

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.