Java多线程中条件判断为啥要用while判断

Java多线程中条件判断

一、使用if判断造成的线程虚假唤醒问题

​ 我们在使用线程时,进行条件判断时,往往会先考虑使用if进行判断,在线程进行等待时就会出现不确定的结果。先来看看两个线程下的操作。

/**
	多线程编程模式
	1、高聚低合前提下,线程操作资源类
	2、判断/干活/通知
	3、防止虚假唤醒
**/
class A {
    private int nums = 0;

    public synchronized void increment() throws InterruptedException {
        //等待条件
        if (nums != 0) {
            //等待
            this.wait();
        }
        nums++;
        System.out.println(Thread.currentThread().getName() + "->=" + nums);
        //通知其它线程,我+1完成了
        this.notifyAll();
    }

    public synchronized void decrement() throws InterruptedException {
        //等待条件
        if (nums == 0) {
            //等待
            this.wait();
        }
        nums--;
        System.out.println(Thread.currentThread().getName() + "->=" + nums);
        //通知其它线程,我-1完成了
        this.notifyAll();
    }
}

public class Test2 {
    public static void main(String[] args) {
        A a = new A();
        new Thread(() -> {
            try {
                for (int i = 0; i < 10; i++) {
                    a.increment();
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }, "A").start();

        new Thread(() -> {
            try {
                for (int i = 0; i < 10; i++) {
                    a.decrement();
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }, "B").start();
    }
}

对于两个线程(一个生产,一个消费)时,可以保证正确结果,原因如下图解析:

在这里插入图片描述

从上图分析可以看到,每次有线程进行生产或者消费时,另一个线程会进入wait状态,直到生产或者消费完成时通知才进行后续操作。所以,对于两个线程时,使用if进行条件判断可以保证正确结果。

二、为啥要使用while来进行条件判定

使用if判断对于多个线程操作时,结果就不一定了,以四个线程为例,结果如下:

在这里插入图片描述

从结果可以得知,nums的结果出现了大于1

原因分析:

​ 在Java程序中,Java语言不能真正的去操纵线程执行,线程实际运行是交由c++去处理的,这是和cpu的时间片算法有关,和操作系统有关。分析见下:

//生产者代码
public synchronized void increment() throws InterruptedException {
  //生产等待条件
  if (nums != 0) {
   //等待
     this.wait();
   }
   nums++;
   System.out.println(Thread.currentThread().getName() + "->=" + nums);
   //通知其它线程,我+1完成了
   this.notifyAll();
}
//消费者代码
public synchronized void decrement() throws InterruptedException {
        //消费等待条件
        if (nums == 0) {
            //等待
            this.wait();
        }
        nums--;
        System.out.println(Thread.currentThread().getName() + "->=" + nums);
        //通知其它线程,我-1完成了
        this.notifyAll();
    }

在这里插入图片描述

所以要使用while来进行条件判断

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值