传统的生产者消费者问题,防止虚假唤醒

本文探讨了面试常考的生产者消费者问题,指出在多线程环境下,简单的if判断可能导致虚假唤醒,进而引入错误结果。通过改用while判断来确保线程安全,防止这种情况发生。
摘要由CSDN通过智能技术生成

面试常见的笔试题:单例模式,八大排序算法,生产者和消费者,死锁

生产者和消费者问题Synchronized版

package com.liao.lesson1;

public class Test02 {
    public static void main(String[] args) {
        A a = new A();
        new Thread(()->{
            for (int i = 0; i < 10; i++) {
                try {
                    a.increment();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"A").start();
        new Thread(()->{
            for (int i = 0; i < 10; i++) {
                try {
                    a.increment();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"B").start();
    }
}
class A{
    private int number;
    public synchronized void increment() throws InterruptedException {
        if (number!=0){
            this.wait();
        }
        number++;
        notifyAll();
        System.out.println(Thread.currentThread().getName()+"--->"+number);
    }
    public synchronized void decrement() throws InterruptedException {
        if (number==0){
            this.wait();
        }
        number--;
        notifyAll();
        System.out.println(Thread.currentThread().getName()+"--->"+number);
    }
}

问题存在:这是两个线程的时候,那有4个线程的时候会出现什么问题?
这个时候通过代码我们发现线程。出错了,线程不安全了,不再只有0,1,两种结果,会出现3,或4。

在这里插入图片描述
使用if一次性判断会出现虚假唤醒问题,解决办法就是将if判断改为while判断

package com.liao.lesson1;

public class Test03 {
    public static void main(String[] args) {
        com.liao.lesson1.A a = new com.liao.lesson1.A();
        new Thread(()->{
            for (int i = 0; i < 10; i++) {
                try {
                    a.increment();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"A").start();
        new Thread(()->{
            for (int i = 0; i < 10; i++) {
                try {
                    a.increment();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"B").start();
        new Thread(()->{
            for (int i = 0; i < 10; i++) {
                try {
                    a.decrement();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"C").start();
        new Thread(()->{
            for (int i = 0; i < 10; i++) {
                try {
                    a.decrement();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"D").start();
    }
}
class B{
    private int number;
    public synchronized void increment() throws InterruptedException {
        while (number!=0){
            this.wait();
        }
        number++;
        notifyAll();
        System.out.println(Thread.currentThread().getName()+"--->"+number);
    }
    public synchronized void decrement() throws InterruptedException {
        while (number==0){
            this.wait();
        }
        number--;
        notifyAll();
        System.out.println(Thread.currentThread().getName()+"--->"+number);
    }
}

while判断的话,就是防止两个线程同时进入,其中一个等待

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Marlboro~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值