Java Multithreading about consumer/producer Design Patterns

Source: Internet
Author: User

1 ImportJavax.swing.plaf.SliderUI;2 3 /*4 * Producer Producter5 * Warehouse Godown6 * Consumer Consumer7 * Producers and consumers build connections through warehouses, where the current number of warehouses is lower than the maximum inventory, producer threads continually produce products (modify the value of the properties of the Godown Class)8 * The number of products in the warehouse is greater than 0 o'clock, and consumers are constantly removing the product (modifying the value of the properties of the Godown Class). When a producer produces a product that equals the maximum inventory value,9 * Stop production, when the product quantity is less than 0 o'clock, consumers stop spendingTen  *      One * Analysis: A * 1 involves multithreading: producers and consumers, and may have multiple producers and multiple consumers - * 2 involving data sharing: The number of products in the warehouse will be jointly operated by multiple producers and consumers - * Producer and producer synchronization between producer and consumer synchronization between consumer and consumer (synchronized) the * 3 Thread-related communication: Producer-to-consumer communication (wait and notify) -  */ -  -  Public classThreaddemo { +      -     /** +      * @paramargs A      */ at      Public Static voidMain (string[] args) { -Godown Godown =NewGodown ();//Create a product warehouse -Consumer cs =NewConsumer (Godown);//Create consumer target objects and share the warehouse with consumers -Producter PD =NewProducter (Godown);//Create and share the producer target object with the warehouse -Thread TR1 =NewThread (PD);//Create a producer thread -Thread TR2 =NewThread (PD);//Create a second producer thread inThread TR3 =NewThread (CS);//Create a consumer thread -Tr1.setname ("one producer Thread");//Modify Thread Name toTr2.setname ("Producer Thread No. Second"); +Tr3.setname ("Number One consumer thread"); -Tr1.start ();//Start Thread the Tr2.start ();  * Tr3.start (); $ Panax Notoginseng     } -  the  + } A /* the * Consumer +  */ - classConsumerImplementsrunnable{ $     PrivateGodown Godown; $     PrivateBoolean Boolea =true; -      PublicConsumer (Godown godown) { -          This. Godown =Godown; the     } - @OverrideWuyi      Public voidRun () {//continuous removal of products the          while(Boolea) { - Godown. Consumption (); Wu         } -          About     } $ } -     /* - * Producer -      */ A classProducterImplementsrunnable{ +      the     PrivateGodown Godown; -     PrivateBoolean Boolea =true; $      Publicproducter (Godown godown) { the          This. Godown =Godown; the     } the @Override the      Public voidrun () { -          while(Boolea) {//continuous production of products in Godown. Production (); the         } the          About     } the      the } the  + classgodown{ -      Public Static Final intmax_size = 100;//Maximum Stock volume the      Public intCurnum;//the current inventory defaults to 0Bayi      Public synchronized voidProduction () {//Production Products the         if(curnum<max_size) {//start production If production conditions are met thecurnum++;  -Notify ();//after the product has been produced, notify the hibernation process to work (if it wakes up to another production line because the common object lock is locked at this time, -                     //so the wake-up process is blocked, and if the consumer thread wakes up, the consumer thread starts to work) theSystem.out.println (Thread.CurrentThread (). GetName () + "production of a product current surplus quantity:" +curnum); the             Try { theThread.CurrentThread (). Sleep (250);//control the speed of production the                  -}Catch(interruptedexception e) { the e.printstacktrace (); the             } the}Else{94              the             Try { theWait ();//when production conditions are not met, the producer thread releases the current object lock and enters the hibernation state the}Catch(interruptedexception e) {98 e.printstacktrace (); About             } -         }101     }102      Public synchronized voidconsumption () {103         if(curnum>0){104              the notify ();106curnum--;107System.out.println (Thread.CurrentThread (). GetName () + "consumption of a product current surplus quantity:" +curnum);108             Try {109Thread.CurrentThread (). Sleep (300); the                 111}Catch(interruptedexception e) { the             113 e.printstacktrace (); the             } the}Else{ the             Try {117 wait ();118}Catch(interruptedexception e) {119                  - e.printstacktrace ();121             }122             123         }124     } the}

Java Multithreading about consumer/producer Design Patterns

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.