Java notify () wait () synchronized producer and consumer case

Source: Internet
Author: User

Too lazy to write, look directly at the code

Mobile Phone class

/*** Mobile Phone type * Maximum number of phones produced *@authorHWUA **/ Public classshouji{ Public intMax//define container Maximum capacity     Public intCurrentnum;//defining the container's current capacity          PublicShouji (intmax) {         This. Max =Max; Currentnum= 0; }}

Production class

//production of mobile phone class Public classSjphone1Implementsrunnable{ PublicShouji con;  PublicSjphone1 (Shouji con) {//constructor Function         This. con =con; }     Public voidrun () { while(true){//Keep Running            synchronized(Con) {//Phone class lock                if(Con.currentnum < Con.max) {//If the current container is dissatisfied, it can produceCon.notify ();//notify and release the lock after productioncon.currentnum++; System.out.println ("Producers are producing Xiaomi mobile phone 1, the current number of products:" +con.currentnum+ "department"); }Else if(Con.currentnum = = Con.max) {//if the production of mobile phone equals the largest definition of mobile phone market capacitySystem.out.println ("Mobile phone production has been saturated, producers stop production, is waiting for consumption ..."); Try{con.wait ();//suspend production and release locks}Catch(interruptedexception e) {e.printstacktrace (); }                }//if else if}//SYN Executes the synchronization block release lock, output two consecutive times the producer is because: after releasing the lock, if there is no waiting for the thread, then the first thread to execute the synchronization block to execute it                         Try{Thread.Sleep (100);//adjust the producer frequency, too quickly and easily sudden death ~ ~}Catch(interruptedexception e) {e.printstacktrace (); }//Try}// while    }}

Consumer Products

//Consumer Products Public classSjphone2Implementsrunnable{ PublicShouji con;  PublicSjphone2 (Shouji con) {//constructor Function         This. con =con; }     Public voidrun () { while(true){//Keep Running            synchronized(Con) {//Mobile phone Lock                if(Con.currentnum > 0) {//when you have a cell phone,Con.notify ();//Buy your phone and release the lockcon.currentnum--; System.out.println ("Consumers are consuming Xiaomi mobile phones sell 1, the current number of products:" +con.currentnum+ "department"); }Else if(Con.currentnum = = 0) {//If you don't have a cell phoneSystem.out.println ("The phone has been bought, consumers stop spending, is waiting for production ..."); Try{con.wait ();//suspend consumption and release locks}Catch(interruptedexception e) {e.printstacktrace (); }                }//else if}//syn                         Try{Thread.Sleep (140);//adjust the consumer frequency, too fast easy to die ~ ~}Catch(interruptedexception e) {e.printstacktrace (); }//Try}// while}//Run}

Test class Main

/*** Test Class*/ Public classdome01{ Public Static voidMain (String args[]) {Shouji container=NewShouji (5);//define the maximum definition of mobile phone market capacity, here is 5Sjphone1 sj1 =NewSjphone1 (container);//the number of mobile phones in the box is synchronized, so the phone object reference is passed as a parameter to the producer and consumerSjphone2 sj2 =NewSjphone2 (container);//Ibid .//Start production consumption mode        NewThread (sj1, "production"). Start (); NewThread (Sj2, "consumption"). Start (); }}

Remember that the lock object must be

Constructor  Correspondence
Shouji class

The same object, or the thread will run, no rules, must be locked up

Java notify () wait () synchronized producer and consumer case

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.