美文网首页
深入多线程3

深入多线程3

作者: 安安静静写代码 | 来源:发表于2017-08-21 17:57 被阅读4次

生产者模式消费者模式
什么叫生产者消费者模式呢,举个例子,一个工厂生产面包最多能生产6个 给超市,超市一次最多只能卖6个,消费者则卖面包当超时面包数量大于0时消费面包

此模式是通过线程间通信完成的,生产者和消费者属于不同的线程,此模式包括以下几个要素
1.产品
2.代理
3.生产者
4.消费者
产品 :面包

package com.qf.demo4;

public class Bread {

    private String brand;
    private int price;
    public Bread(String brand, int price) {
        super();
        this.brand = brand;
        this.price = price;
    }
    public Bread() {
        super();
    }
    public String getBrand() {
        return brand;
    }
    public void setBrand(String brand) {
        this.brand = brand;
    }
    public int getPrice() {
        return price;
    }
    public void setPrice(int price) {
        this.price = price;
    }
    @Override
    public String toString() {
        return "Bread [brand=" + brand + ", price=" + price + "]";
    }
    
    
}

代理:超市

package com.qf.demo4;

public class Markt {

    Bread[] breads = new Bread[6];
    int index = -1;// 标志 一个面包也没有
    // 生产
    public synchronized void  product(Bread bread){
        // 面包是6 的话就停止生产
        if(index==5){
            try {
                this.wait();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        // 想要向数组中添加一个面包
        index++;
        breads[index] = bread;
        System.out.println("生产者生产了一个面包"+bread);
        this.notify();
        
        // 不足六个 就生产
    }
    // 消费
    public synchronized void sale(){
        // 面包个数大于0 的时候就 消费
        if(index==-1){
            try {
                this.wait();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        System.out.println("消费者消费了一个面包"+breads[index]);
        index--;
        this.notify();
        // 不足的0 的时候 就等待
    }
}

生产者

package com.qf.demo4;

public class Facotory implements Runnable{

    Markt markt;
    public Facotory(Markt markt) {
        this.markt = markt;
    }
    
    @Override
    public void run() {
        for (int i = 1; i <= 100; i++) {
            markt.product(new Bread("二狗"+i, i));
        }
        
    }
    
}

消费者

package com.qf.demo4;

public class Custmer implements Runnable{
    Markt markt;
    public Custmer(Markt markt) {
        this.markt = markt;
    }
    
    @Override
    public void run() {
        for (int i = 0; i < 100; i++) {
            markt.sale();
        }
    }
}

测试类

package com.qf.demo4;
/**
 * 生产者消费者模型
 * 1 产品
 * 2 超市(代理)
 * 3 生产者
 * 4 消费者
 * 线程间通信
 * 
 * @author Administrator
 *
 */
public class Test {

    public static void main(String[] args) {
        
        Markt markt= new Markt();
        
        Facotory facotory = new Facotory(markt);
        Custmer custmer = new Custmer(markt);
        
        
        Thread thread = new Thread(facotory);
        Thread thread2 = new Thread(custmer);
        
        thread.start();
        thread2.start();
    }
}

相关文章

网友评论

      本文标题:深入多线程3

      本文链接:https://www.haomeiwen.com/subject/hslhdxtx.html