生产者消费者模型

生产者消费者模型

一、一个生产者 一个消费者

/**步骤:
*     1.生产者线程和消费者线程操作同一个资源
*        脏数据:
*           null -- 0.0
*           华为 -- 0.0
*
*     2.两个产品之间来回切换 -- 目的:让步骤1的问题更加明显
*        脏数据:
*           null -- 0.0
*           华为 -- 0.0
*           小米 -- 3999.0
*           华为 -- 1999.0
*        解决方法:加锁 -> 生产者线程在生产的过程中(setBrand()和setPrice()),消费者线程不能输出
*        脏数据:
*           null -- 0.0
*
*     3.生产一个,消费一个 -> 生产者线程和消费者线程来回切换
*/
public static void main(String[] args) {
​
        Phone phone = new Phone();//null -- 0.0
​
        Producer p1 = new Producer(phone);
        Producer p2 = new Producer(phone);
        Consumer c1 = new Consumer(phone);
        Consumer c2 = new Consumer(phone);
​
        p1.start();
        p2.start();
        c1.start();
        c2.start();
}

产品类--Phone

​
public class Phone {
​
    private String brand;
    private double price;
    private boolean isStore;
    
    public Phone() {
    }
​
    public Phone(String brand, double price) {
        this.brand = brand;
        this.price = price;
    }
​
    public String getBrand() {
        return brand;
    }
​
    public void setBrand(String brand) {
        this.brand = brand;
    }
​
    public double getPrice() {
        return price;
    }
​
    public void setPrice(double price) {
        this.price = price;
    }
​
    public boolean isStore() {
        return isStore;
    }
​
    public void setStore(boolean isStore) {
        this.isStore = isStore;
    }
    
}
​

生产者线程类 -- Producer

​
//生产者线程
public class Producer extends Thread{
​
    private Phone phone;
​
    public Producer(Phone phone) {
        this.phone = phone;
    }
​
    private static boolean flag = true;
​
    @Override
    public void run() {
​
        while(true){
            synchronized (phone) {
                if(phone.isStore()){//有库存
                    try {
                        phone.wait();//等待:1.将当前线程记录到对象监视器中 2.释放锁资源  3.当前线程进入到阻塞状态
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                if(flag){
                    phone.setBrand("华为");
                    phone.setPrice(3999);
                }else{
                    phone.setBrand("小米");
                    phone.setPrice(1999);
                }
                flag = !flag;
​
                phone.setStore(true);
                phone.notify();//唤醒对象监视器中所有的线程
            }
        }
    }
}
​
​

消费者线程类 -- Consumer

​
//消费者线程
public class Consumer extends Thread{
​
    private Phone phone;
​
    public Consumer(Phone phone) {
        this.phone = phone;
    }
​
    @Override
    public void run() {
        while(true){
            synchronized (phone) {
                if(!phone.isStore()){//没有库存
                    try {
                        phone.wait();//等待:1.将当前线程记录到对象监视器中 2.释放锁资源  3.当前线程进入到阻塞状态
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                System.out.println(phone.getBrand() + " -- " + phone.getPrice());
​
                phone.setStore(false);
                phone.notify();
            }
        }
    }
}
​

二、多个生产者 多个消费者

生产者线程类 -- Producer

//生产者线程
public class Producer extends Thread{
​
    private Phone phone;
​
    public Producer(Phone phone) {
        this.phone = phone;
    }
​
    private static boolean flag = true;
​
    @Override
    public void run() {
​
        while(true){
            synchronized (phone) {
                while(phone.isStore()){//有库存
                    try {
                        phone.wait();//等待:1.将当前线程记录到对象监视器中 2.释放锁资源  3.当前线程进入到阻塞状态
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                if(flag){
                    phone.setBrand("华为");
                    phone.setPrice(3999);
                }else{
                    phone.setBrand("小米");
                    phone.setPrice(1999);
                }
                flag = !flag;
​
                phone.setStore(true);
                phone.notifyAll();//唤醒对象监视器中所有的线程
            }
        }
    }
}
​
​

消费者线程类 -- Consumer

//消费者线程
public class Consumer extends Thread{
private Phone phone;
​
public Consumer(Phone phone) {
    this.phone = phone;
}
​
@Override
public void run() {
    while(true){
        synchronized (phone) {
            while(!phone.isStore()){//没有库存
                try {
                    phone.wait();//等待:1.将当前线程记录到对象监视器中 2.释放锁资源  3.当前线程进入到阻塞状态
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println(phone.getBrand() + " -- " + phone.getPrice());
​
            phone.setStore(false);
            phone.notifyAll();
        }
    }
}
}
  • 24
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值