Start and stop of Java line layer

Source: Internet
Author: User

classdo8{ Public Static voidMain (string[] args) {Resource R=NewResource (); Input in=NewInput (R); Output out=NewOutput (R); Thread T1=NewThread (in); Thread T2=NewThread (out);            T1.start ();    T2.start (); }}classresource{String name;    String sex; Booleanflag=false;}//inputclassInputImplementsrunnable{Resource R; Input (Resource r) { This. r=S; }     Public voidrun () {intX=0;  while(true)        {        synchronized(r) {if(r.flag) {Try{r.wait ();}Catch(Exception e) {}//when True, the current line layer stops                }               if(x==0) {R.name= "Go Inside"; R.sex= "Male"; }                     Else{r.name= "Xiaoli"; R.sex= "Wumen"; } R.flag=true; R.notify ();//start any of the stopped line layers} x= (x+1)%2; }    }}//OutputclassOutputImplementsrunnable{Resource R; Output (Resource r) { This. r=R; }     Public voidrun () { while(true)        {            synchronized(r) {if(!r.flag) {Try{r.wait ();}Catch(Exception e) {}//when not true, the current line layer stops                    }                    Try{thread.sleep (100);}Catch(Exception e) {} System.out.println (R.name+"..."+r.sex); R.flag=false; R.notify ();//start any of the stopped line layers                                    }        }    }}

Start and stop of Java line layer

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.