A brief introduction to the producer and consumer model in concurrent Java programming

  • 2020-04-01 04:05:05
  • OfStack

An overview of the
The producer and consumer model is a classic model for multithreaded programs. More accurately, it should be called "producer-consumer-warehouse model". Without warehouses, producers and consumers lack Shared storage space, and there are no non-collaborative problems.

The sample
Define a scenario. A warehouse is only allowed to hold 10 items. The producer can put one item into it at a time, and the consumer can take one item out of it at a time. At the same time, we need to pay attention to the following four points:
1.   Only one producer can be produced at a time, and production methods need to be synchronized.
2.   Only one consumer can consume at a time, and the method of consumption needs to be synchronized.
3.   When the warehouse is empty, the consumer cannot continue to consume. Before consuming, the consumer needs to loop to determine whether the current warehouse state is empty or not. If it is empty, the consuming thread needs to wait, and the lock is released to allow other synchronization methods to execute.
4.   When the warehouse is full, the producer cannot continue production. The producer needs to loop to determine whether the current warehouse state is full. If the warehouse is full, the production thread needs to wait.

The sample code is as follows:

   


 public class Concurrence { 
    public static void main(String[] args) { 
      WareHouse wareHouse = new WareHouse(); 
      Producer producer = new Producer(wareHouse); 
      Consumer consumer = new Consumer(wareHouse); 
   
      new Thread(producer).start(); 
      new Thread(consumer).start(); 
    } 
  } 
   
  class WareHouse { 
    private static final int STORE_SIZE = 10; 
    private String[] storeProducts = new String[STORE_SIZE]; 
    private int index = 0; 
   
    public void pushProduct(String product) { 
      synchronized (this) { 
        while (index == STORE_SIZE) { 
          try { 
            this.wait(); 
          } catch (InterruptedException e) { 
            e.printStackTrace(); 
          } 
        } 
   
        storeProducts[index++] = product; 
        this.notify(); 
   
        System.out.println(" Production of the : " + product + " ,  In the warehouse at present : " + index 
            + "  A goods "); 
      } 
    } 
   
    public synchronized String getProduct() { 
      synchronized (this) { 
        while (index == 0) { 
          try { 
            this.wait(); 
          } catch (InterruptedException e) { 
            e.printStackTrace(); 
          } 
        } 
   
        String product = storeProducts[index - 1]; 
        index--; 
        System.out.println(" The consumption : " + product + ",  In the warehouse at present : " + index 
            + "  A goods "); 
        this.notify(); 
        return product; 
      } 
    } 
  } 
   
  class Producer implements Runnable { 
    WareHouse wareHouse; 
   
    public Producer(WareHouse wh) { 
      this.wareHouse = wh; 
    } 
   
    @Override 
    public void run() { 
      for (int i = 0; i < 40; i++) { 
        String product = "product" + i; 
        this.wareHouse.pushProduct(product); 
      } 
    } 
  } 
   
  class Consumer implements Runnable { 
    WareHouse wareHouse; 
   
    public Consumer(WareHouse wh) { 
      this.wareHouse = wh; 
    } 
   
    @Override 
    public void run() { 
      for (int i = 0; i < 40; i++) { 
        this.wareHouse.getProduct(); 
      } 
    } 
  } 


Related articles: