Java: producer and consumer issues
I remember the first time I made a java question, I saw "Writing producer and consumer problems" and thought it was related to the factory model. Now, I think it's a crash.
Java producer and consumer issues are actually about multi-thread concurrent operations on the same resource buffer. When the resource buffer is full and the thread continues to add data, it should wait and send a message notification when there is space; when there is no resource in the resource buffer and the thread continues to fetch data, it should wait for it. If there is a resource, a Message notification will be sent again;
Let's take a look at the run:
The following code is used:
Main. java: (Main class)
Package com. vrinux. setandget; public class Main {public static void main (String [] args) {// TODO Auto-generated method stub // create a store object and set the maximum capacity to 10; store store = new Store (10); // create two producers; Pro pro1 = new Pro (store); Pro pro2 = new Pro (store); // create three consumers; coner coner1 = new Coner (store); Coner coner2 = new Coner (store); Coner coner3 = new Coner (store); // start the thread; pro1.start (); pro2.start (); coner1.start (); coner2.start (); coner3.start ();}}
Store. java: (Store class)
Package com. vrinux. setandget; public class Store {// static variable, maximum Store storage value; private final int max; // initial Store storage value; 0; private int count; // constructor; public Store (int max) {this. max = max; this. count = 0;} // method of purchasing goods. [note] Use synchronized to modify it. This is a synchronization lock. public synchronized void add () {// determines whether the number of stores is full; yes, let the producer wait; while (count> = max) {System. out. println ("the Warehouse is full. Please import it later ~ "); Try {this. wait ();} catch (InterruptedException e) {// TODO Auto-generated catch block e. printStackTrace () ;}/// if it is not full, purchase 1; count + = 1; // print the current inventory; System. out. println ("current inventory is" + count); // notify all producers that they can purchase goods. this. policyall () ;}// method of consumption. [note] Use synchronized to modify it. This is a synchronization lock. public synchronized void remove () {// determines whether the store stock is out of stock, yes, the consumer waits. 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 block e. printStackTrace () ;}/// otherwise, consume 1; count-= 1; // print the inventory; System. out. println ("current inventory is" + count); // notify all consumers to consume; this. policyall ();}}
Pro. java: (producer)
Package com. vrinux. setandget; public class Pro extends Thread {private Store store; public Pro (Store store) {this. store = store ;}@ Override public void run () {// TODO Auto-generated method stub while (true) {// purchase from store and sleep; store. add (); try {Thread. sleep (610);} catch (InterruptedException e) {// TODO Auto-generated catch block e. printStackTrace ();}}}}
Coner. java: (consumer)
Package com. vrinux. setandget; public class Coner extends Thread {private Store store; public Coner (Store store) {this. store = store ;}@ Override public void run () {// TODO Auto-generated method stub while (true) {// consume from the store and sleep; store. remove (); try {Thread. sleep (1000);} catch (InterruptedException e) {// TODO Auto-generated catch block e. printStackTrace ();}}}}