What is the Producer/consumer model?
A module is responsible for generating the data, which is handled by another module (the module here is generalized, which can be classes, functions, threads, processes, etc.). The module that produces the data is visually called the producer, and the module that processes the data is called the Consumer. Between producers and consumers in the addition of a buffer zone, our image is called the warehouse, the producer is responsible for the warehouse into the goods, and consumers are responsible for the goods from the warehouse, which constitutes the producer of consumer Models. The structure diagram is as Follows:
There are several advantages to the producer consumer Model:
1. decoupling
Due to the existence of buffers, there is no direct dependence between producers and consumers, and the degree of coupling decreases.
2. Support concurrency
Since producers and consumers are two separate concurrent bodies, they are connected using buffers as bridges, producers can continue to produce the next data only by throwing data into the buffer, and consumers only need to take the data from the buffer, so that they do not block because of each other's processing Speed.
3. Support free and busy uneven
The buffer also has another benefit. If the speed of manufacturing data is fast and slow, the benefits of the buffer are Reflected. When the data is manufactured fast, the consumer is too late to handle it, and the unhandled data can be temporarily present in the Buffer. And so the producers slow down the production, consumers and then slowly dispose of.
The Producer-consumer model should be accurately described as a "producer-warehousing-consumer" model, which follows the following rules:
1, the producer only in the storage is not full time production, warehouse full Stop Production.
2, consumers only in storage products can be consumed, Cang is waiting.
3, when the consumer found that the storage of products can be consumed when the producers will be informed of Production.
4, producers in the production of consumable products, should inform the waiting consumers to spend
This model will be combined with Java.lang.Object's wait and notify, notifyall methods to achieve the above Requirements. The instance code is as Follows:
Create so-called "warehouses," which are (essentially, collectively accessed) shared data regions
//This class is a shared data region (essentially: Common Access) public classSyncstack {Privatestring[] str =NewString[10]; Private intindex; //for producer calls public synchronized voidpush (String Sst) {if(index = =Sst.length ()) { Try{wait (); } Catch(interruptedexception E) {//TODO auto-generated Catch blockE.printstacktrace (); } } this. Notify ();//wake up a single thread waiting on this object monitorstr[index] =sst; Index++; } //for consumer calls public synchronizedString pop () {if(index = = 0){ Try{wait (); } Catch(interruptedexception E) {//TODO auto-generated Catch blockE.printstacktrace (); } } this. Notify (); Index--; String Product=str[index]; returnproduct; } //is to define a method that returns an array of values, and returns a string[] reference publicstring[] Pro () {returnstr; }}
Create a Producer:
public classProducterImplementsRunnable {PrivateSyncstack stack; publicproducter (syncstack Stack) { this. Stack =stack; } public voidRun () { for(inti = 0;i<stack.pro (). length;i++) {String Producter= "product" +i; Stack.push (producter); System.out.println ("produced:" +producter); Try{thread.sleep (200); } Catch(interruptedexception E) {//TODO auto-generated Catch blockE.printstacktrace (); } } }}
Create a Consumer:
public classConsumerImplementsRunnable {PrivateSyncstack stack; publicConsumer (syncstack Stack) { this. Stack =stack; } public voidRun () { for(intI=0;i<stack.pro (). length;i++) {String Consumer=Stack.pop (); System.out.println ("consumed:" +consumer); Try{thread.sleep (400); } Catch(interruptedexception E) {//TODO auto-generated Catch blockE.printstacktrace (); } } }}
Test Class:
public class testdeam { publicstaticvoid main (string[] Args) { New syncstack (); New Consumer (stack); New producter (stack); New Thread (p). start (); New Thread (c). start (); }}
Test Results:
Production: Product 0 consumption: product 0 produced: product 1 production: product 2 consumption: product 2 produced: product 3 consumed: product 3 produced: product 4 produced: product 5 produced: product 6 consumed: product 5 produced: product 7 consumed: product 6 Consumed: product 7 Produced: products in the 8: Product 9 consumption: 8 Consumer: Product 9 consumption: product 4 consumption: Product 1
Producer/consumer patterns of Java design patterns