多线程中的虚假唤醒问题

虚假唤醒问题的案例

我们创建两个线程A和B,让A打印1,B打印0,每个线程循环十次打印,我们先看第一版代码:

public class ThreadDemo1 {
    public static void main(String[] args) {
        Share share = new Share();
        new Thread(()->{
            try {
                for (int i = 0; i < 30; i++) {
                    share.incr();
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        },"线程A").start();

        new Thread(()->{
            try {
                for (int i = 0; i < 30; i++) {
                    share.decr();
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        },"线程B").start();
    }
}


//创建资源类
class Share{
    private int num = 0;
    //+1的方法
    public synchronized void incr() throws InterruptedException {
        //第二步:判断,干活,通知、
        if(num != 0){
            this.wait();
        }
        num++;
        System.out.println(Thread.currentThread().getName()+"::"+num);
        //通知其他线程
        this.notifyAll();
    }

    //-1的方法
    public synchronized void decr() throws InterruptedException {
        if(num != 1){
            this.wait();
        }
        num--;
        System.out.println(Thread.currentThread().getName()+"::"+num);
        this.notifyAll();
    }
}
  • 我们看一下结果:
    在这里插入图片描述

上面的效果和我们的预期完全一样,但当我们有四个线程去执行,也就是A线程打印1,B线程打印0,C线程打印1,D线程打印0的时候,就出现了问题:

在这里插入图片描述

这里产生虚假唤醒问题的根本原因就是我们的条件判断使用的是if,因为使用wait方法不仅仅会释放自己的锁,而且当其还有一个特性就是在哪里沉睡就在哪里醒来,所以如果使用if来判断当前值是否为1和0时,当其处于wait时,如果被唤醒,就不会继续去判断了,而是继续执行,使用应该把if换成while.第二版代码如下:

package com.ctvit.sync;

/**
 * @author ctvit
 */
public class ThreadDemo1 {
    public static void main(String[] args) {
        Share share = new Share();
        new Thread(()->{
            try {
                for (int i = 0; i < 30; i++) {
                    share.incr();
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        },"线程A").start();

        new Thread(()->{
            try {
                for (int i = 0; i < 30; i++) {
                    share.decr();
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        },"线程B").start();
        new Thread(()->{
            try {
                for (int i = 0; i < 30; i++) {
                    share.incr();
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        },"线程C").start();

        new Thread(()->{
            try {
                for (int i = 0; i < 30; i++) {
                    share.decr();
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        },"线程D").start();
    }
}


//创建资源类
class Share{
    private int num = 0;
    //+1的方法
    public synchronized void incr() throws InterruptedException {
        //第二步:判断,干活,通知、
        while (num != 0){
            this.wait();
        }
        num++;
        System.out.println(Thread.currentThread().getName()+"::"+num);
        //通知其他线程
        this.notifyAll();
    }

    //-1的方法
    public synchronized void decr() throws InterruptedException {
        while (num != 1){
            this.wait();
        }
        num--;
        System.out.println(Thread.currentThread().getName()+"::"+num);
        this.notifyAll();
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值