java classic producer consumer model (with source code)

I. Introduction

  Producers and consumers is a classic problem of threading model. There are two ways to solve the producer / consumer problem: First, using a mechanism to protect the synchronization between producers and consumers; second is to establish a pipeline between producers and consumers. The first embodiment has a higher efficiency, and easy to implement, can be controlled better code, more commonly used. The second pipe is difficult to control the buffer, the data object is transferred easily to the package, etc., practicality is not strong.

  This article describes the use of synchronization mechanisms to achieve the producer / consumer problem.

Second, the code

  1, Product product categories:

/*
 * 产品类
 * @author gongyu *
*/ public class Product { private int id; private String name; public Product(int id, String name) { this.id = id; this.name = name; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "Product [id=" + id + ", name=" + name + "]"; } }

  2, Class Library room ProductHouse

/ * 
 * Library room class 
 * * / 
public  class ProductHouse {
     Private Product [] Products;
     Private  int max;
     Private  int index = -1 ;
     public ProductHouse ( int max) {
         // the TODO Auto-Generated constructor Stub 
        the this .max = max ; 
        products = new new product [max]; 
    } 
    / * producer generation product storage method * / 
    public  the synchronized  void Push (product product) {
         the while (index> = max. 1-) {
             The try {
                 the this .wait (); 
            } the catch (InterruptedException E) {
                 // the TODO Auto-Generated Block the catch 
                e.printStackTrace (); 
            } 
        } 
        the this .notifyAll (); // wake other threads 
        products [++ index ] = product; 
    } 
    / * consumers obtain product * / 
    public  the synchronized product POP () {
         the while (index <0 ) {
             the try {
                 the this .wait (); 
            }catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        this.notifyAll();
        Product p=products[index--];
        return p;
        
    }
}

  3, the producer thread ProductThread

/**
 * 生产者线程 ProductThread
 * @author gongyu
 *
 */
public class ProductThread extends Thread{
    private ProductHouse productHouse;
    public ProductThread(ProductHouse productHouse,String name) {
        // TODO Auto-generated constructor stub
        super(name);
        this.productHouse=productHouse;
    }
    
    public void run() {
        for(int i=0;i<10;i++) {
            Product p=new Product(i+1,this.getName()+"馒头");
            
            System.out.println(this.getName()+"生产了"+p);
            productHouse.push(p);
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        
    }
}

  4, consumer thread CustomerThread

/**
 * 消费者线程 CustomerThread
 * @author gongyu
 *
 */
public class CustomerThread extends Thread{
    private ProductHouse productHouse;
    public CustomerThread(ProductHouse productHouse,String name) {
        // TODO Auto-generated constructor stub
        super(name);
        this.productHouse=productHouse;
    }
    public void run() {
        for(int i=0;i<5;i++) {
            Product p=productHouse.pop();
            System.out.println(this.getName()+"消费了"+p);
        }
        try {
            Thread.sleep(300);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

 

  5, the test class

public class Test01 {
    public static void main(String[] args) {
        ProductHouse ph=new ProductHouse(30);
        ProductThread p1=new ProductThread(ph,"老张");
        ProductThread p2=new ProductThread(ph,"老李");
        ProductThread p3=new ProductThread(ph,"老杨");
        ProductThread p4=new ProductThread(ph,"老王");
        
        CustomerThread c1=new CustomerThread(ph,"小红");
        CustomerThread c2=new CustomerThread(ph,"小绿");
        CustomerThread c3=new CustomerThread(ph,"小蓝");
        CustomerThread c4=new CustomerThread(ph,"小白");
        CustomerThread c5=new CustomerThread(ph,"小黑");
        CustomerThread c6=new CustomerThread(ph,"小灰");
        p1.start();p2.start();p3.start();p4.start();
        c1.start();c2.start();c3.start();c4.start();c5.start();c6.start();
    }
}

 

Guess you like

Origin www.cnblogs.com/choua1997/p/11570183.html