Java Train station ticketing procedure (thread wait)

Source: Internet
Author: User

1  PackageCom.java.thread.synch;2 3 /**4 * Sell Tickets Demo5  * @authorAdministrator6 * Two stations sell 100 tickets7  */8  Public classSellticket {9     StaticObject obj=NewObject ();Ten     Static intcount=100; One      Public Static voidMain (string[] args) { A //Stationone one =new stationone (obj); - //stationtwo =new stationtwo (obj); - //thread th1=new thread (one); the //thread Th2=new thread (both); - //Th1.start (); - //Th2.start (); -  +Thread th1=NewThread (NewA ()); -Thread th2=NewThread (NewA ()); +Th1.setname ("Station One"); ATh2.setname ("Station II"); at Th1.start (); - Th2.start (); -     } - } -  - classAImplementsrunnable{ in //string Obj=new string ("AA"); -@SuppressWarnings ("Static-access") to      Public voidrun () { +String obj= "AAA";//cannot be a string Obj=new string ("AA"); AA is stored in the heap area - //there are two obj, but they are all pointing to the same aaa,aaa stored in the data area, the data area has only one copy the          while(true){ *             synchronized(obj) { $                 if(sellticket.count>0){Panax Notoginseng                     Try { -Thread.CurrentThread (). Sleep (30); the}Catch(interruptedexception e) { + //TODO auto-generated Catch block A e.printstacktrace (); the                     } + //System.out.printf ("%s sold%d tickets \ n", Thread.CurrentThread (). GetName (), sellticket.count); -System.out.println (Thread.CurrentThread (). GetName () +sellticket.count); $sellticket.count--; $}Else{ -                      Break; -                 } the             } - Wuyi         } the     } - } Wu  -  About /** $ * Station One -  * @authorAdministrator -  * -  */ A classStationoneImplementsrunnable{ +     PrivateObject obj; the      Publicstationone (Object obj) { -          This. obj=obj; $     } the@SuppressWarnings ("Static-access") the      Public voidrun () { the         synchronized(obj) { the              while(sellticket.count>0){ - obj.notify (); in                 Try { theThread.CurrentThread (). Sleep (60); the}Catch(interruptedexception E1) { About e1.printstacktrace (); the                 } theSYSTEM.OUT.PRINTF ("\t\t\t\t station sold the first%d tickets \ n", sellticket.count); thesellticket.count--; +                 Try { - obj.wait (); the}Catch(interruptedexception e) {Bayi e.printstacktrace (); the                 } the             } - obj.notify (); -         } the     } the } the  the /** - * Station two the  * @authorAdministrator the  * the  */94 classStationtwoImplementsrunnable{ the     PrivateObject obj; the      Publicstationtwo (Object obj) { the          This. obj=obj;98     } About@SuppressWarnings ("Static-access") -      Public voidrun () {101         synchronized(obj) {102              while(sellticket.count>0){103 obj.notify ();104                 Try { theThread.CurrentThread (). Sleep (60);106}Catch(interruptedexception E1) {107 e1.printstacktrace ();108                 }109SYSTEM.OUT.PRINTF ("station two sold the first%d tickets \ n", sellticket.count); thesellticket.count--;111                 Try { the obj.wait ();113}Catch(interruptedexception e) { the e.printstacktrace (); the                 } the             }117 obj.notify ();118         }119     } -}

Java Train station ticketing procedure (thread wait)

Related Article

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.