Java multi-thread Implementation of producer consumer issues (wait/consumer y)
This article uses synchronized and the wait/notify method of the object for some examples on the Internet.
First, define three classes. One is the container class. The attributes include the maximum container capacity and the current capacity;
The other two are producer and consumer classes, respectively. The two are production methods and consumption methods (both are implemented in their respective run methods. In fact, it is best to separate them into a separate method)
Package test3; class Container {public int max; // defines the maximum Container capacity public int currentNum; // defines the current Container capacity public Container (int max) {this. max = max; currentNum = 0 ;}} class Producer implements Runnable {public Container con; public Producer (Container con) {this. con = con;} public void run () {while (true) {// There are countless Apple synchronized (con) {if (con. currentNum <con. max) {// if the current container is not satisfied, the con can be produced. Y (); // notification after production and release the lock con. currentNum ++; System. out. Println ("the producer is producing... + 1, current product count: "+ con. currentNum);} else if (con. currentNum = con. max) {// System. out. println ("the box is saturated. The producer stops production and is waiting for consumption... "); try {con. wait ();} catch (InterruptedException e) {e. printStackTrace () ;}/// if else if} // After the syn completes the synchronization block release lock, the output result shows two consecutive producers because: After the lock is released, if there is no waiting Thread, execute the synchronization block of the Thread to which it is first executed. try {Thread. sleep (100); // adjust the producer frequency so that it is too fast and prone to sudden death ~~} Catch (InterruptedException e) {e. printStackTrace ();} // try} // while} class Consumer implements Runnable {public Container con; public Consumer (Container con) {this. con = con;} public void run () {while (true) {synchronized (con) {if (con. currentNum> 0) {con. running y (); con. currentNum --; System. out. println ("the consumer is consuming... -1, current product count: "+ con. currentNum);} else if (con. currentNum = 0) {System. out. println ("the box is empty, the consumer stops consumption, waiting Waiting for production... "); try {con. wait ();} catch (InterruptedException e) {e. printStackTrace () ;}// else if} // syntry {Thread. sleep (140); // adjust the consumer frequency so that it is too fast to survive ~~} Catch (InterruptedException e) {e. printStackTrace ();} // try} // while} // run} public class TestProducerConsumer {public static void main (String args []) {Container container = new Container (5); // defines the maximum capacity of the box. Here, 5 Producer producer = new Producer (container); // synchronize the number of apples in the box, therefore, the box object reference is passed as a form parameter to the producer and Consumer consumer Consumer = new Consumer (container); // new Thread (producer, "producer "). start (); // start the production consumption mode new Thread (consumer, "consumer "). start ();}}
Program output:
The producer is producing... + 1, current product count: 1 consumer is consuming... -1, current number of products: 0 the producer is producing... + 1, current product count: 1 consumer is consuming... -1, current number of products: 0 the producer is producing... + 1, current product count: 1 consumer is consuming... -1, current number of products: 0 the producer is producing... + 1, current product count: 1 consumer is consuming... -1, current number of products: 0 the producer is producing... + 1. Number of current products: 1. the producer is producing... + 1, current product count: 2 consumers are consuming... -1. Number of current products: 1. the producer is producing... + 1, current product count: 2 consumers are consuming... -1. Number of current products: 1. the producer is producing... + 1, current product count: 2 consumers are consuming... -1. Number of current products: 1. the producer is producing... + 1. Number of current products: 2. The producer is producing... + 1, current product count: 3 consumers are consuming... -1, current number of products: 2 the producer is producing... + 1, current product count: 3 consumers are consuming... -1, current number of products: 2 the producer is producing... + 1, current product count: 3 consumers are consuming... -1, current number of products: 2 the producer is producing... + 1, current product count: 3 consumers are consuming... -1, current number of products: 2 the producer is producing... + 1. Current product quantity: 3. The producer is producing... + 1, current product count: 4 consumers are consuming... -1. Current product quantity: 3. The producer is producing... + 1, current product count: 4 consumers are consuming... -1. Current product quantity: 3. The producer is producing... + 1, current product count: 4 consumers are consuming... -1. Current product quantity: 3. The producer is producing... + 1, current product count: 4 producers in production... + 1, current product count: 5 consumers are consuming... -1. Number of current products: 4. The producer is producing... + 1, current product count: 5 consumers are consuming... -1. Number of current products: 4. The producer is producing... + 1, current product count: 5 consumers are consuming... -1. Number of current products: 4. The producer is producing... + 1, current product count: 5 consumers are consuming... -1. Number of current products: 4. The producer is producing... + 1. Current product quantity: 5 boxes are saturated. The producer stops production and is waiting for consumption... the consumer is consuming... -1. Number of current products: 4. The producer is producing... + 1, current product count: 5 consumers are consuming... -1. Number of current products: 4. The producer is producing... + 1, current product count: 5 consumers are consuming... -1. Number of current products: 4. The producer is producing... + 1, current product count: 5 consumers are consuming... -1. Number of current products: 4. The producer is producing... + 1. Current product quantity: 5 boxes are saturated. The producer stops production and is waiting for consumption... the consumer is consuming... -1. Number of current products: 4. The producer is producing... + 1, current product count: 5 consumers are consuming... -1. Number of current products: 4. The producer is producing... + 1, current product count: 5 consumers are consuming... -1. Number of current products: 4. The producer is producing... + 1, current product count: 5 consumers are consuming... -1. Number of current products: 4. The producer is producing... + 1. Current product quantity: 5 boxes are saturated. The producer stops production and is waiting for consumption... the consumer is consuming... -1. Number of current products: 4. The producer is producing... + 1, current product count: 5 consumers are consuming... -1. Number of current products: 4. The producer is producing... + 1, current product count: 5 consumers are consuming... -1. Number of current products: 4. The producer is producing... + 1, current product count: 5 consumers are consuming... -1. Number of current products: 4. The producer is producing... + 1. Current product quantity: 5 boxes are saturated. The producer stops production and is waiting for consumption... the consumer is consuming... -1. Number of current products: 4. The producer is producing... + 1, current product count: 5 consumers are consuming... -1. Number of current products: 4. The producer is producing... + 1, current product count: 5 consumers are consuming... -1. Number of current products: 4. The producer is producing... + 1, current product count: 5 consumers are consuming... -1. Number of current products: 4. The producer is producing... + 1. Current product quantity: 5 boxes are saturated. The producer stops production and is waiting for consumption... the consumer is consuming... -1. Number of current products: 4. The producer is producing... + 1, current product count: 5 consumers are consuming... -1. Number of current products: 4. The producer is producing... + 1, current product count: 5