java多线程生产者消费者案例、虚假唤醒问题

package com.producersAndConsumers;

public class Test {
    public static void main(String[] args) {
        SalesPerson salesPerson = new SalesPerson();
        Product product = new Product(salesPerson);
        Consumer consumer = new Consumer(salesPerson);
        new Thread(product,"生产者A").start();
        new Thread(consumer,"消费者B").start();
    }
}
// 店员类,produc表示产品的数量,get()方法为生产的方法,sale()方法为消费的方法,两个方法都使用synchronized关键字修饰确保线程安全
class SalesPerson{
    private int product = 0 ;
    public synchronized void get(){
    	// 当产品数量达到20时,仓库存满,方法调用wait方法使生产者停止生产
        if (product>=20){
            System.out.println("已满");
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        // ++product使产品+1
        System.out.println(Thread.currentThread().getName()+" : "+(++product));
        // 生产出产品后唤醒消费者线程,
        this.notifyAll();
    }
    public synchronized void sale(){
        if (product<=0){
            System.out.println("缺货");
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println(Thread.currentThread().getName()+"消费了"+(--product));
        this.notifyAll();
    }
}
//生产者类
class Product implements Runnable{
    private SalesPerson salesPerson ;
    public Product(SalesPerson salesPerson){
        this.salesPerson = salesPerson;
    }
    @Override
    public void run() {
        for (int i = 0; i < 20; i++) {
            try {
                Thread.sleep(200);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            salesPerson.get();
        }
    }
}
// 消费者
class Consumer implements Runnable{
    private SalesPerson salesPerson ;
    public Consumer(SalesPerson salesPerson){
        this.salesPerson = salesPerson;
    }
    @Override
    public void run() {
        for (int i = 0; i < 20 ; i++) {
            salesPerson.sale();
        }
    }
}

运行结果:
在这里插入图片描述
生产者消费者交替进行生产消费,看似很和谐,但是当有多个生产者和消费者同时运行时:

public class Test {
    public static void main(String[] args) {
        SalesPerson salesPerson = new SalesPerson();
        Product product = new Product(salesPerson);
        Consumer consumer = new Consumer(salesPerson);
        new Thread(product,"生产者A").start();
        new Thread(consumer,"消费者B").start();
        new Thread(product,"生产者C").start();
        new Thread(consumer,"消费者D").start();
    }
}

在这里插入图片描述
这时运行结果就变得十分离谱,甚至出现了负数,这就是虚假唤醒问题

虚假唤醒问题

假设一开始处于缺货状态,此时两个消费者线程都在等待,如果一个生产者线程抢到资源完成一次生产,就会同时唤醒全部的消费者线程,两个消费者线程同时执行--product的操作,导致product的数量变为负数,这种现象称为虚假唤醒问题

对于解决虚假唤醒问题,JDK中给出了说明:对于wait()方法,必须始终在循环中使用:
在这里插入图片描述

// 修改后的代码
class SalesPerson{
    private int product = 0 ;
    public synchronized void get(){
        //if (product>=20){
        while (product>=20){
            System.out.println("已满");
            try {
                this.wait();
            } catch (InterruptedException e) { }
        }
        System.out.println(Thread.currentThread().getName()+" : "+(++product));
        this.notifyAll();
    }
    public synchronized void sale(){
        while (product<=0){
            System.out.println("缺货");
            try {
                this.wait();
            } catch (InterruptedException e) {}
        }
        System.out.println(Thread.currentThread().getName()+" : "+(--product));
        this.notifyAll();
    }
}

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值