Thread Multithreading Stopsleep order problem

Source: Internet
Author: User

Today, learning javase Multi-threading, sleeping sleep inside the problem is a bit disturbing:

 Public classthread_06_stopsleep{ Public Static voidMain (string[] args) throws exception{Processor p=NewProcessor (); Thread T=NewThread (P); T.start ();//Thread.Sleep ( the);//Thread : Represents the same class as the current code, main ()         for(intI=0; I <Ten; ++i) {System. out. println (Thread.CurrentThread (). GetName () +"----->"+i); } P.run=false; }}classProcessor implements runnable{Boolean run=true;  Public voidrun () { for(intI=0; I <Ten; ++i) {            if(run) {//if run is false, then sleep will not execute.                Try{Thread.Sleep ( +); }Catch(Exception e) {e.printstacktrace (); } System. out. println (Thread.CurrentThread (). GetName () +"----->"+i); }Else{System. out. println ("sleep, was awakened." ); return;//terminates the function, which is where the current thread is killed.            }        }    }

But why did the next one come true when the output was finished? As follows:

Why there is a fifth time, not the fifth time has been false in the pass, seeking expert guidance maze!!

Yes, and I also because it is also a beginner, the article did not have any technical content, you want to reprint on the casual turn, but please add my link, OK?

Thread Multithreading Stopsleep order problem

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.