为了账号安全,请及时绑定邮箱和手机立即绑定

线程中生产者和消费者问题---虚假唤醒

标签:
Java

话不多说,上代码

  1. 用synchronized + if 实现,这时只有AB两个线程时没有问题
package com.example.demo02;

/**
 * 注释
 *
 * @author sunhao
 * @Date 2021-08-30-21:41
 */
public class Test03 {

    public static void main(String[] args) {
        Data data = new Data();

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

// 判断等待, 业务, 通知
class Data{
    private int num = 0;

    /**
     * 生产者
     * @throws InterruptedException
     */
    public synchronized void increment() throws InterruptedException {
        if (num != 0) {
            // 等待
            this.wait();
        }
        // 业务
        num ++;
        System.out.println(Thread.currentThread().getName() + "=>" + num);
        // 通知
        this.notifyAll();
    }

    /**
     * 消费者
     * @throws InterruptedException
     */
    public synchronized void decrement() throws InterruptedException {
        if (num == 0) {
            // 等待
            this.wait();
        }
        // 业务
        num --;
        System.out.println(Thread.currentThread().getName() + "=>" + num);
        // 通知
        this.notifyAll();
    }
}

实验结果
图片描述

  1. 用synchronized + if 实现,但是有AC(生产者) BD(消费者)四个线程
    假如 BD 的if判断了 num == 0 这时候两个线程都去wait了 这时 A 执行了 num ++ 并且notifyAll() 唤醒了 BD 然后BD一起往下走 执行了num – 就出现了负数的情况
package com.example.demo02;

/**
 * 注释
 *
 * @author sunhao
 * @Date 2021-08-30-21:41
 */
public class Test03 {

    public static void main(String[] args) {
        Data data = new Data();

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

// 判断等待, 业务, 通知
class Data{
    private int num = 0;

    /**
     * 生产者
     * @throws InterruptedException
     */
    public synchronized void increment() throws InterruptedException {
        if (num != 0) {
            // 等待
            this.wait();
        }
        // 业务
        num ++;
        System.out.println(Thread.currentThread().getName() + "=>" + num);
        // 通知
        this.notifyAll();
    }

    /**
     * 消费者
     * @throws InterruptedException
     */
    public synchronized void decrement() throws InterruptedException {
        if (num == 0) {
            // 等待
            this.wait();
        }
        // 业务
        num --;
        System.out.println(Thread.currentThread().getName() + "=>" + num);
        // 通知
        this.notifyAll();
    }
}

先看一下错误示例
图片描述

  1. 用synchronized + while 实现,有AC(生产者) BD(消费者)四个线程
    假如 BD 的while判断了 num == 0 这时候两个线程都去wait了 这时 A 执行了 num ++ 并 且notifyAll() 唤醒了 BD 然后BD一起往下走 B执行了num-- 变成了0 这时D线程的while条件还是num==0 继续等待 结果正确

具体原因
因为if只会执行一次,执行完会接着向下执行if()外边的
而while不会,直到条件满足才会向下执行while()外边的

package com.example.demo02;

/**
 * 注释
 *
 * @author sunhao
 * @Date 2021-08-30-21:41
 */
public class Test03 {

    public static void main(String[] args) {
        Data data = new Data();

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

// 判断等待, 业务, 通知
class Data{
    private int num = 0;

    /**
     * 生产者
     * @throws InterruptedException
     */
    public synchronized void increment() throws InterruptedException {
        while (num != 0) {
            // 等待
            this.wait();
        }
        // 业务
        num ++;
        System.out.println(Thread.currentThread().getName() + "=>" + num);
        // 通知
        this.notifyAll();
    }

    /**
     * 消费者
     * @throws InterruptedException
     */
    public synchronized void decrement() throws InterruptedException {
        while (num == 0) {
            // 等待
            this.wait();
        }
        // 业务
        num --;
        System.out.println(Thread.currentThread().getName() + "=>" + num);
        // 通知
        this.notifyAll();
    }
}

图片描述

  1. 用Lock + while 实现,有AC(生产者) BD(消费者)四个线程
package com.example.demo02;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 注释
 *
 * @author sunhao
 * @Date 2021-08-30-21:41
 */
public class Test04 {

    public static void main(String[] args) {
        Data2 data2 = new Data2();

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

// 判断等待, 业务, 通知
class Data2{
    private int num = 0;

    // Lock 锁 替代 synchronized
    Lock lock = new ReentrantLock();
    // 一个Condition只为一个业务服务
    final Condition incrementCondition = lock.newCondition();
    final Condition decrementCondition = lock.newCondition();
//    condition.await();    wait
//    condition.signalAll();    notifyAll

    /**
     * 生产者
     * @throws InterruptedException
     */
    public void increment() throws InterruptedException {
        lock.lock();
        try {
            while (num != 0) {
                // 等待
                incrementCondition.await();
            }
            // 业务
            num ++;
            System.out.println(Thread.currentThread().getName() + "=>" + num);
            // 通知
            decrementCondition.signalAll();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }

    /**
     * 消费者
     * @throws InterruptedException
     */
    public void decrement() throws InterruptedException {
        lock.lock();
        try {
            while (num == 0) {
                // 等待
                decrementCondition.await();
            }
            // 业务
            num --;
            System.out.println(Thread.currentThread().getName() + "=>" + num);
            // 通知
            incrementCondition.signalAll();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }
}

图片描述

还有一个最关键的问题没有解决 不是按ABCD这个顺序执行的
???????????????????????

点击查看更多内容
1人点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消