第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定

線程中生產(chǎn)者和消費(fèi)者問(wèn)題---虛假喚醒

標(biāo)簽:
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这个顺序执行的
???????????????????????

點(diǎn)擊查看更多內(nèi)容
1人點(diǎn)贊

若覺(jué)得本文不錯(cuò),就分享一下吧!

評(píng)論

作者其他優(yōu)質(zhì)文章

正在加載中
感謝您的支持,我會(huì)繼續(xù)努力的~
掃碼打賞,你說(shuō)多少就多少
贊賞金額會(huì)直接到老師賬戶
支付方式
打開(kāi)微信掃一掃,即可進(jìn)行掃碼打賞哦
今天注冊(cè)有機(jī)會(huì)得

100積分直接送

付費(fèi)專欄免費(fèi)學(xué)

大額優(yōu)惠券免費(fèi)領(lǐng)

立即參與 放棄機(jī)會(huì)
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)

舉報(bào)

0/150
提交
取消