Java: Producer Consumer issues

Source: Internet
Author: User

Remember the first time to do the Java problem, see "write producer consumer problems", but also think it is related to the factory model. Now it is thunder to think about it.
Java's producer consumer problem is the multi-threaded concurrent operation of the same resource buffer, when the resource buffer is full, the thread continues to add data, you should make it wait, there is space to send a message notification, when the resource buffer does not have resources, the thread continues to fetch data, should make it wait, there is a resource is re-send message notification;

First look at the run:

Here's the code:
Main.java: (Main Class)

Package com.vrinux.setandget; Public classMain { Public Static void Main(string[] args) {//TODO auto-generated method stub        //Create a store object and set the maximum capacity to ten;Store store =NewStore (Ten);//creation of two producers;Pro Pro1 =NewPro (store); Pro Pro2 =NewPro (store);//creation of three consumers;Coner Coner1 =NewConer (store); Coner Coner2 =NewConer (store); Coner Coner3 =NewConer (store);//start thread;Pro1.start ();        Pro2.start ();        Coner1.start ();        Coner2.start ();    Coner3.start (); }}

Store.java: (Store Class)

Package com.vrinux.setandget; Public classStore {//static variable, store's maximum storage value;    PrivateFinalintMaxthe initial value of store storage; 0;    Private intCount//constructor;     Public Store(intMax) { This. max = max; This. Count =0; }//Arrival method, "note" to be decorated with synchronized, this is a synchronous lock,     PublicSynchronizedvoid Add() {//To determine whether the store is full of storage and to allow the producer to wait;         while(Count>=max) {System. out. println ("The warehouse is full, please enter the goods a little later ~");Try{ This. Wait (); }Catch(Interruptedexception e) {//TODO auto-generated catch blockE.printstacktrace (); }        }//Not full, then the arrival of 1;Count + =1;//print current inventory;System. out. println ("Current inventory is"+count);//Inform all producers that the goods can be purchased;         This. Notifyall (); }//Consumption method, "note" to be decorated with synchronized, this is a synchronous lock,     PublicSynchronizedvoid Remove() {//To determine whether the store inventory is not in stock, is to let consumers wait;         while(count<=0) {System. out. println ("The warehouse is out of stock, please pick up the goods later.");Try{ This. Wait (); }Catch(Interruptedexception e) {//TODO auto-generated catch blockE.printstacktrace (); }        }//otherwise, consume 1;Count-=1;//print inventory;System. out. println ("Current inventory is"+count);//Notify all consumers to consume;         This. Notifyall (); }}

Pro.java: (producer)

 PackageCom.vrinux.setandget; Public  class Pro extends Thread{    PrivateStore store; Public Pro(store) { This. store = store; }@Override     Public void Run() {//TODO auto-generated method stub         while(true){//To the store and hibernate;Store.add ();Try{Thread.Sleep (610); }Catch(Interruptedexception e) {//TODO auto-generated catch blockE.printstacktrace (); }        }    }}

Coner.java: (consumer)

 PackageCom.vrinux.setandget; Public  class coner extends Thread {    PrivateStore store; Public Coner(store) { This. store = store; }@Override     Public void Run() {//TODO auto-generated method stub         while(true) {//consume from the store, and hibernate;Store.remove ();Try{Thread.Sleep ( +); }Catch(Interruptedexception e) {//TODO auto-generated catch blockE.printstacktrace (); }        }    }}

Java: Producer Consumer issues

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.