java线程虚假唤醒_【Java多线程】多线程虚假唤醒

public class CustomerDemo {

public static void main(String[] args) {

/**

* 要求一个线程增加,一个线程减少

*/

Number number= new Number();

new Thread(() -> {

for (int i = 0; i < 10 ;i ++) {

try {

number.decrease();

} catch (InterruptedException e) {

e.printStackTrace();

}

}

}, "A").start();

new Thread(() -> {

for (int i = 0; i < 10 ;i ++) {

try {

number.increase();

} catch (InterruptedException e) {

e.printStackTrace();

}

}

}, "B").start();

new Thread(() -> {

for (int i = 0; i < 10 ;i ++) {

try {

number.decrease();

} catch (InterruptedException e) {

e.printStackTrace();

}

}

}, "C").start();

new Thread(() -> {

for (int i = 0; i < 10 ;i ++) {

try {

number.increase();

} catch (InterruptedException e) {

e.printStackTrace();

}

}

}, "D").start();

}

}

/**

* 线程之间的通信

*/

class Number {

private int num = 0;

public synchronized void decrease() throws InterruptedException {

if (num == 0) {

this.wait();

}

num --;

System.out.println(Thread.currentThread().getName() + " ... " + num);

this.notifyAll();

}

public synchronized void increase() throws InterruptedException {

if (num != 0) {

this.wait();

}

num ++;

System.out.println(Thread.currentThread().getName() + " ... " + num);

this.notifyAll();

}

}

上面程序最理想的状态是0 ,1,0,1间隔执行,但是多执行几次就有可能会出现以下情形

B ... 1

A ... 0

B ... 1

.....

.....

A ... -1

A ... -2

A ... -3

A ... -4

这是因为多线程的虚假唤醒导致的,在多线程中判断必须使用while

如果使用if

if (num != 0) {

// 假设此时有B,D线程在这里wait,如果被唤醒后,不会再次进行num != 0判断,这样就会导致num ++ 两次。如果使用while的话,线程被唤醒以后会再次进行条件判断

this.wait();

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值