A brief introduction to producer and consumer models in Java concurrent programming _java

Source: Internet
Author: User

Overview
for multithreaded routines, the producer and consumer models are very classic models. More accurately, it should be called "producer-consumer-warehouse model". Leaving the warehouse, producers, consumers are missing a shared storage space, there is no collaboration is not a problem.

Example
define a scene. A warehouse allows only 10 items to be stored, each time a producer can put a product in it, and the consumer can take a product out of it each time. At the same time, attention should be paid to the following 4 points:
1. Only one producer can produce at the same time, and the production method requires a lock synchronized.
2. At the same time only one consumer consumption, consumption methods need to lock synchronized.
3. When the warehouse is empty, the consumer cannot continue to consume. Consumers need to cycle before consumption to determine whether the current warehouse state is empty, empty then the consumer thread needs to wait, release the lock to allow other synchronous methods to execute.
4. When the warehouse is full, the producer cannot continue to produce, the producer produces the money needs to circulate to determine whether the current warehouse status is full, then the production line needs wait and the release lock allows other synchronous methods to execute.

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; The public void Pushproduct (String product) {synchronized (.) {while (index = = store_size) {T 
          ry {this.wait (); 
          catch (Interruptedexception e) {e.printstacktrace (); 
        } storeproducts[index++] = product; 
   
        This.notify (); 
      System.out.println ("Production:" + Product + ", the current warehouse altogether:" + index + "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 ("Consumption:" + Product + ", the current warehouse altogether:" + index + "goods"); 
        This.notify (); 
      return product; 
   
    }} class Producer implements Runnable {WareHouse WareHouse; 
    Public Producer (WareHouse wh) {this.warehouse = WH;  @Override public void Run () {(int i = 0; i < 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 () {(int i = 0; i < i++) { This.wareHouse.getProduct (); 
 } 
    } 
  }

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.