2021-04-21

Java多线程之虚假唤醒

个人理解:虚假唤醒就是唤醒了多个线程,但是不满足生产者或者消费者执行的条件拿到锁后都分别去执行wait后面的代码,然后出现异常并且会发生阻塞现象
虚假唤醒的代码:

public class ProduceConsumeSyn {
    public static void main(String[] args) {
        Data t = new Data();
        new Thread(()->{
            for (int i = 0; i < 10; i++) {
                try {
                    t.increase(i);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"生产者1").start();
        new Thread(()->{
            for (int i = 0; i < 10; i++) {
                try {
                    t.decrease(i);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"消费者1").start();
        new Thread(()->{
            for (int i = 0; i < 10; i++) {
                try {
                    t.increase(i);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"生产者2").start();

        new Thread(()->{
            for (int i = 0; i < 10; i++) {
                try {
                    t.decrease(i);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"消费者2").start();

    }
}
class Data{
    private  int num = 0;
    public synchronized void increase(int index) throws InterruptedException {
        if (num!=0){   //虚假唤醒问题症结
            System.out.println(Thread.currentThread().getName()+"等待减少,num:"+num+"-------------->index:"+index);
            this.wait();  //不等于0,等待消费
        }
        num++;
            System.out.println(Thread.currentThread().getName()+"----------->"+num+"-------------->index:"+index);
        //通知其他线程  我已经加1生产完了,唤醒其他 线程
        this.notifyAll();
    }

    public synchronized void decrease(int index) throws InterruptedException {
        if (num==0){  //虚假唤醒问题症结
            System.out.println(Thread.currentThread().getName()+"等待增加,num:"+num+"-------------->index:"+index);
            this.wait();  //等于0,等待生产
        }
        num--;
            System.out.println(Thread.currentThread().getName()+"----------->"+num+"-------------->index:"+index);
        //通知其他线程  我已经-1消费完了,唤醒其他 线程
        this.notifyAll();
    }
}

运行结果
可以在这里插入图片描述
可以看到还在运行中,按道理每个线程循环完10次后就结束了,说明有线程没被唤醒还在wait,
解决方案之一将if换成while(可能有其他的解决方法但是暂时我就知道这一个,欢迎评论讨论)
在jdk帮助文档没理解为什么换成while就不会存在虚假唤醒了,后来花了点时间想清楚了
在这里插入图片描述

在jdk帮助文档的wait方法介绍里有这么一句话: 一旦获得了对象的控制,其对对象的所有同步声明就恢复到现状 - 也就是在调用wait方法之后的情况,就是说在调用notifyAll方法后,所有的被唤醒的线程会执行wait方法后面的代码,出了if的大括号不会判断直接往下走了,这就是问题症结。
换成while后的运行结果

class Data{
    private  int num = 0;
    public synchronized void increase(int index) throws InterruptedException {
        while (num!=0){
            System.out.println(Thread.currentThread().getName()+"等待减少,num:"+num+"-------------->index:"+index);
            this.wait();  //不等于0,等待消费
        }
        num++;
            System.out.println(Thread.currentThread().getName()+"----------->"+num+"-------------->index:"+index);
        //通知其他线程  我已经加1生产完了,唤醒其他 线程
        this.notifyAll();
    }

    public synchronized void decrease(int index) throws InterruptedException {
        while (num==0){
            System.out.println(Thread.currentThread().getName()+"等待增加,num:"+num+"-------------->index:"+index);
            this.wait();  //等于0,等待生产
        }
        num--;
            System.out.println(Thread.currentThread().getName()+"----------->"+num+"-------------->index:"+index);
        //通知其他线程  我已经-1消费完了,唤醒其他 线程
        this.notifyAll();
    }
}

运行结果,经过N次测试没出现过虚假唤醒的情况,而且所有的线程也都停了
在这里插入图片描述

主要就是wait后面的代码继续往下执行if这种情况不会在判断,假如两个消费线程都被唤醒然后抢占锁,各个唤醒的线程分别拿到锁后就去执行if大括号下面的代码,直接消费了,导致出现问题(经过测试也发现假如消费者2拿到锁,大多数情况下会执行多次在满足情况的条件下,虚假唤醒的运行结果也可以看到消费者2在num是-1此时满足条件会不停的执行循环直到停下来),那么换成while后,因为wait在while里面,两个线程同时被唤醒,第二个线程拿到锁后它会判断一次,因为第一个被唤醒的线程抢到锁更改后,第二个线程就满足等待的条件了也就进入wait了,所以保证不会出现虚假唤醒问题

个人拙见,欢迎评论!!!
转载请注明出处

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值