Java Threads: Concurrent collaboration-producer consumer models

Source: Internet
Author: User
Tags thread

In fact, it should be the "producer-consumer-warehousing" model, leaving the warehouse, the producer consumer model is not convincing.

For this model, you should be clear about the following points:

1, the producers only in the warehouse is not full time production, warehouse full stop production.

2, the consumer only in the warehouse has the product time can consume, Cang waits.

3, when the consumer found no products can be consumed when the production will inform producers.

4, producers in the production of consumer products, should inform the waiting consumers to spend.

This model will be combined with Java.lang.Object wait and notify, Notifyall method to achieve the above requirements. This is very important.

/**


* Java Thread: Concurrent collaboration-producer consumer model


*


* @author leizhimin


*/


public class Test {


public static void Main (string[] args) {


Godown godown = new Godown (30);


Consumer c1 = new Consumer (m, Godown);


Consumer c2 = new Consumer (Godown);


Consumer C3 = New Consumer (Godown);


Producer p1 = new Producer (ten, Godown);


Producer p2 = new Producer (ten, Godown);


Producer p3 = new Producer (ten, Godown);


Producer P4 = new Producer (ten, Godown);


Producer P5 = new Producer (ten, Godown);


Producer P6 = new Producer (ten, Godown);


Producer P7 = new Producer (n, Godown);


C1.start ();


C2.start ();


C3.start ();


P1.start ();


P2.start ();


P3.start ();


P4.start ();


P5.start ();


P6.start ();


P7.start ();


         }


}


/**


* Warehouse


*/


class Godown {


public static final int max_size = 100; Max Stock


public int curnum; Current Inventory


Godown () {


         }


godown (int curnum) {


this.curnum = Curnum;


         }


         /**


* Production of a specified number of products


          *


* @param neednum


          */


public synchronized void produce (int neednum) {


//test if production
is required

while (Neednum + curnum > max_size) {


System.out.println ("The number of products to be produced" + Neednum + "Excess inventory" + (Max_size-curnum) + ", temporarily unable to perform production tasks!" );


try {


//Current production line waiting for


wait ();


catch (Interruptedexception e) {


E.printstacktrace ();


                         }


                 }


//Meet the production conditions, then the production, where the simple change of the current inventory


Curnum + = Neednum;


System.out.println ("has produced" + Neednum + "A product, is the storage quantity is" + curnum);


//Wake up all threads waiting on this object monitor


Notifyall ();


         }


         /**


* Consumption of a specified number of products


          *


* @param neednum


          */


public synchronized void consume (int neednum) {


//test to be consumable


while (Curnum < neednum) {


try {


//Current production line waiting


wait ();


} catch (Interruptedexception e) {


E.printstacktrace ();


                         }


                 }


//Meet the consumption conditions, then the consumption, here simple change the current inventory


Curnum-= Neednum;


System.out.println ("has already consumed" + Neednum + "A product, is the storage quantity is" + curnum);


//Wake up all threads waiting on this object monitor


Notifyall ();


         }


}


/**


* Producer


*/


class Producer extends Thread {


private int neednum; Number of products produced


private Godown Godown; Warehouse


Producer (int neednum, Godown godown) {


this.neednum = Neednum;


This.godown = Godown;


         }


public void Run () {


//production of a specified number of products


godown.produce (Neednum);


         }


}


/**


* Consumer


*/


class Consumer extends Thread {


private int neednum; Number of products produced


private Godown Godown; Warehouse


Consumer (int neednum, Godown godown) {


this.neednum = Neednum;


This.godown = Godown;


         }


public void Run () {


//consumption of a specified number of products


Godown.consume (Neednum);


         }


}

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.