前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【JUC基础】07. wait(),notify()虚假唤醒

【JUC基础】07. wait(),notify()虚假唤醒

作者头像
有一只柴犬
发布2024-01-25 11:02:13
960
发布2024-01-25 11:02:13
举报
文章被收录于专栏:JAVA体系JAVA体系

1、什么是wait和notify

属于Object类的两个方法。

wait(),使当前线程等待,直到另一个线程使用notify()或notifyAll()方法唤醒。

notify(),唤醒正在wait等待的线程。

官方API文档说明:

2、 示例代码

既然说明了wait()和notify()的功能,那么我们手撸一段代码来试下:

代码语言:javascript
复制
/**
 * @author Shamee loop
 * @date 2023/4/9
 */
public class ObjectWaitNotifyDemo {

    public static void main(String[] args) {
        NumberOper object = new NumberOper();

        new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                object.add();
            }
        }, "thread-add-1").start();

        new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                object.sub();
            }
        }, "thread-sub-1").start();
    }
}

/**
 * 定义一个数量操作类
 * 其中包含两个方法,
 * 一个是对临界值number++;当判断number!=0,就等待number变成0后在执行。同时唤醒另一个线程
 * 另一个方法是对临界值number--;当判断number==0,就等待number!=0后在执行。同时唤醒另一个线程
 * 同时两个方法分别加了synchronized锁,确保线程安全问题
 */
class NumberOper {
    private int number = 0;

    public synchronized void add() {
        if(number != 0) {
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        number++;
        System.out.println("线程" + Thread.currentThread().getName() + "执行了add(),number====>" + number);
        this.notifyAll();
    }

    public synchronized void sub() {
        if(number == 0) {
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        number--;
        System.out.println("线程" + Thread.currentThread().getName() + "执行了sub(),number====>" + number);
        this.notifyAll();
    }

}

预期的结果应该是:

线程tread-add-1执行,判断到number==0,就number++;这时候控制台打印number==>1

线程tread-sub-1执行,判断到number==0,则进行等待;等到tread-add-1执行完毕后,唤醒了tread-sub-1执行,这时候tread-sub-1就执行了number--操作;这时候控制台打印number==>0

实际执行结果:

 果然是这样。那么这篇文章就结束了吗?

3、多个线程执行

上面操作示例,只开启了2个线程。但如果开启多个线程呢?我们再试下:

代码语言:javascript
复制
/**
 * @author Shamee loop
 * @date 2023/4/9
 */
public class ObjectWaitNotifyDemo {

    public static void main(String[] args) {
        NumberOper object = new NumberOper();

        new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                object.add();
            }
        }, "thread-add-1").start();

        new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                object.sub();
            }
        }, "thread-sub-1").start();

        new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                object.add();
            }
        }, "thread-add-2").start();

        new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                object.sub();
            }
        }, "thread-sub-2").start();
    }
}

/**
 * 定义一个数量操作类
 * 其中包含两个方法,
 * 一个是对临界值number++;当判断number!=0,就等待number变成0后在执行。同时唤醒另一个线程
 * 另一个方法是对临界值number--;当判断number==0,就等待number!=0后在执行。同时唤醒另一个线程
 * 同时两个方法分别加了synchronized锁,确保线程安全问题
 */
class NumberOper {
    private int number = 0;

    public synchronized void add() {
        if(number != 0) {
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        number++;
        System.out.println("线程" + Thread.currentThread().getName() + "执行了add(),number====>" + number);
        this.notifyAll();
    }

    public synchronized void sub() {
        if(number == 0) {
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        number--;
        System.out.println("线程" + Thread.currentThread().getName() + "执行了sub(),number====>" + number);
        this.notifyAll();
    }

}

预期的结果应该是:

线程tread-add-1执行,判断到number==0,就number++;这时候控制台打印number==>1

线程tread-sub-1执行,判断到number==0,则进行等待;等到tread-add-1执行完毕后,唤醒了tread-sub-1执行,这时候tread-sub-1就执行了number--操作;这时候控制台打印number==>0

实际执行结果:

 很明显结果不对了,甚至出现了负数等值。这是为啥?

4、虚假唤醒

我们来看下官方API文档怎么说:

而文档给出来的建议是,临界值的判断使用while。我们再试下:

代码语言:javascript
复制
public synchronized void add() {
        while(number != 0) {
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        number++;
        System.out.println("线程" + Thread.currentThread().getName() + "执行了add(),number====>" + number);
        this.notifyAll();
    }

    public synchronized void sub() {
        while(number == 0) {
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        number--;
        System.out.println("线程" + Thread.currentThread().getName() + "执行了sub(),number====>" + number);
        this.notifyAll();
    }

执行结果:

 似乎一切正常了。那么是为什么呢?用if不行用while就可以。

什么是虚假唤醒:是不想唤醒它或者说不确定是否应该唤醒,但是被唤醒了。对程序来说,wait 方法应该卡住当前程序,不应该往后执行;但是实际上并没有被卡住,而是在非预期的时间程序正常执行了,没有程序没有被卡住就是被虚假唤醒了

5、结论

在if条件判断下,他只会判断一次,如果这时候被notify唤醒之后,程序会从当前等待的代码继续执行(也就是进入了if判断之后的代码)。而如果使用while判断,唤醒后会重新执行while循环条件。如果条件成立,就继续wait。因此就出现了上面看到的现象。

好了,又白嫖了一个无聊的小知识,没用但有趣的小知识。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2023-05-16,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1、什么是wait和notify
  • 2、 示例代码
  • 3、多个线程执行
  • 4、虚假唤醒
  • 5、结论
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档