Java coordinated synchronization of threads

Source: Internet
Author: User

Example12_8.java

 Public class example12_8 {   publicstaticvoid  main (String args[]) {      new  tickethouse ();      Thread Zhangfei,likui;       New Thread (officer);       Zhangfei.setname ("Zhang Fei")      ; New Thread (officer);        Likui.setname ("Li Kui");      Zhangfei.start ();      Likui.start ();   }}

Tickethouse.java

 Public classTickethouseImplementsRunnable {intFiveamount=2,tenamount=0,twentyamount=0;  Public voidrun () {if(Thread.CurrentThread (). GetName (). Equals ("Zhang Fei") {saleticket (20); }      Else if(Thread.CurrentThread (). GetName (). Equals ("Li Kui") {saleticket (5); }   }   Private synchronized voidSaleticket (intMoney ) {       if(money==5) {//if the thread that is using this method passes a parameter of 5, you do not have to waitFiveamount=fiveamount+1; System.out.println ("To" +thread.currentthread (). GetName () + "entry volume," +Thread.CurrentThread (). GetName ()+ "The money is just right"); }       Else if(money==20) {                     while(fiveamount<3) {            Try{System.out.println ("\ n" +thread.currentthread (). GetName () + "pull aside ...");       Wait (); //if the thread that uses the method passes the parameter is 20 must waitSystem.out.println ("\ n" +thread.currentthread (). GetName () + "continue to buy tickets"); }            Catch(Interruptedexception e) {}} Fiveamount=fiveamount-3; Twentyamount=twentyamount+1; System.out.println ("To" +thread.currentthread (). GetName () + "entry volume," +Thread.CurrentThread (). GetName ()+ "Give 20, find redemption 15 yuan");   } notifyall (); } }

Bank.java

 Public classBankImplementsRunnable {intmoney=200;  Public voidSetmoney (intN) { money=N; }    Public voidrun () {if(Thread.CurrentThread (). GetName (). Equals ("Accounting")) Saveortake (300); Else if(Thread.CurrentThread (). GetName (). Equals ("Cashier")) Saveortake (150);; }     Public synchronized voidSaveortake (intAmount) {//access Method      if(Thread.CurrentThread (). GetName (). Equals ("Accounting")) {          for(inti=1;i<=3;i++) { money=MONEY+AMOUNT/3;//every deposit AMOUNT/3, a little restSystem.out.println (Thread.CurrentThread (). GetName () + "deposit" +amount/3+ ", the account has" +money+ "million, rest and then save"); Try{thread.sleep (1000);//The cashier still cannot use the Saveortake method             }                                    Catch(Interruptedexception e) {}}} Else if(Thread.CurrentThread (). GetName (). Equals ("Cashier")) {          for(inti=1;i<=3;i++) {//The cashier uses the Access method to remove theMONEY=MONEY-AMOUNT/3;//every AMOUNT/3, take a break.System.out.println (Thread.CurrentThread (). GetName () + "Take Out" +amount/3+ "there is a" +money+ "on the account, rest for a while and then fetch"); Try{thread.sleep (1000);//then the accountant still can't use the Saveortake method             }                                      Catch(Interruptedexception e) {}}} }}

Java coordinated synchronization of threads

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.