7、JUC:Lock版的生产者和消费者问题

学习过程观看视频:[狂神说Java]
https://www.bilibili.com/video/BV1B7411L7tE?p=2
欢迎大家支持噢,很良心的老师了!

和synchronized的比较

在这里插入图片描述

java代码

package com.zjl;

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

/**
 * Created by zjl
 * 2020/11/20
 **/
/*
* 1、线程之间的通信问题:生产者和消费者问题!
* 2、线程交替执行,A  B  操作同一个变量  num = 0
* 3、A  num+1
* 4、B  num-1
* */
public class Test2 {
    public static void main(String[] args) {
        Data2 data = new Data2();
        new Thread(()->{
            for (int i = 0; i < 10; i++) {
                try {
                    data.increment();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
        new Thread(()->{
            for (int i = 0; i < 10; i++) {
                try {
                    data.decrement();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
        new Thread(()->{
            for (int i = 0; i < 10; i++) {
                try {
                    data.increment();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
        new Thread(()->{
            for (int i = 0; i < 10; i++) {
                try {
                    data.decrement();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
}


//等待-----》业务-----》通知
class Data2{    //数字,资源类

    private int number = 0;

    Lock lock = new ReentrantLock();
    Condition condition = lock.newCondition();

    //+1
    public void increment() throws InterruptedException {
        lock.lock();
        try {
            while (number!=0){
                //等待
                condition.await();
            }
            number++;
            System.out.println("我加一完毕了+++++++++++++++++++++++++唤醒其他线程减一" + number);
            condition.signalAll();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }

    //-1
    public void decrement() throws InterruptedException {
        lock.lock();
        try {
            while (number == 0){
                //等待
                condition.await();
            }
            number--;
            System.out.println("我减一完毕了--------------------------唤醒其他线程加一" + number);
            condition.signalAll();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }

    }

}

执行结果

/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/bin/java "-javaagent:/Users/zhangjianlong/Library/Application Support/JetBrains/Toolbox/apps/IDEA-U/ch-0/193.5662.53/IntelliJ IDEA.app/Contents/lib/idea_rt.jar=60339:/Users/zhangjianlong/Library/Application Support/JetBrains/Toolbox/apps/IDEA-U/ch-0/193.5662.53/IntelliJ IDEA.app/Contents/bin" -Dfile.encoding=UTF-8 -classpath /Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/charsets.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/deploy.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/ext/cldrdata.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/ext/dnsns.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/ext/jaccess.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/ext/jfxrt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/ext/localedata.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/ext/nashorn.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/ext/sunec.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/ext/sunjce_provider.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/ext/sunpkcs11.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/ext/zipfs.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/javaws.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/jce.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/jfr.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/jfxswt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/jsse.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/management-agent.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/plugin.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/resources.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/rt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/lib/ant-javafx.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/lib/dt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/lib/javafx-mx.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/lib/jconsole.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/lib/packager.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/lib/sa-jdi.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/lib/tools.jar:/Users/zhangjianlong/testCode/JUC-study/target/classes:/Users/zhangjianlong/apache-maven-3.6.3/repository/org/projectlombok/lombok/1.18.8/lombok-1.18.8.jar com.zjl.Test2
我加一完毕了+++++++++++++++++++++++++唤醒其他线程减一1
我减一完毕了--------------------------唤醒其他线程加一0
我加一完毕了+++++++++++++++++++++++++唤醒其他线程减一1
我减一完毕了--------------------------唤醒其他线程加一0
我加一完毕了+++++++++++++++++++++++++唤醒其他线程减一1
我减一完毕了--------------------------唤醒其他线程加一0
我加一完毕了+++++++++++++++++++++++++唤醒其他线程减一1
我减一完毕了--------------------------唤醒其他线程加一0
我加一完毕了+++++++++++++++++++++++++唤醒其他线程减一1
我减一完毕了--------------------------唤醒其他线程加一0
我加一完毕了+++++++++++++++++++++++++唤醒其他线程减一1
我减一完毕了--------------------------唤醒其他线程加一0
我加一完毕了+++++++++++++++++++++++++唤醒其他线程减一1
我减一完毕了--------------------------唤醒其他线程加一0
我加一完毕了+++++++++++++++++++++++++唤醒其他线程减一1
我减一完毕了--------------------------唤醒其他线程加一0
我加一完毕了+++++++++++++++++++++++++唤醒其他线程减一1
我减一完毕了--------------------------唤醒其他线程加一0
我加一完毕了+++++++++++++++++++++++++唤醒其他线程减一1
我减一完毕了--------------------------唤醒其他线程加一0
我加一完毕了+++++++++++++++++++++++++唤醒其他线程减一1
我减一完毕了--------------------------唤醒其他线程加一0
我加一完毕了+++++++++++++++++++++++++唤醒其他线程减一1
我减一完毕了--------------------------唤醒其他线程加一0
我加一完毕了+++++++++++++++++++++++++唤醒其他线程减一1
我减一完毕了--------------------------唤醒其他线程加一0
我加一完毕了+++++++++++++++++++++++++唤醒其他线程减一1
我减一完毕了--------------------------唤醒其他线程加一0
我加一完毕了+++++++++++++++++++++++++唤醒其他线程减一1
我减一完毕了--------------------------唤醒其他线程加一0
我加一完毕了+++++++++++++++++++++++++唤醒其他线程减一1
我减一完毕了--------------------------唤醒其他线程加一0
我加一完毕了+++++++++++++++++++++++++唤醒其他线程减一1
我减一完毕了--------------------------唤醒其他线程加一0
我加一完毕了+++++++++++++++++++++++++唤醒其他线程减一1
我减一完毕了--------------------------唤醒其他线程加一0
我加一完毕了+++++++++++++++++++++++++唤醒其他线程减一1
我减一完毕了--------------------------唤醒其他线程加一0
我加一完毕了+++++++++++++++++++++++++唤醒其他线程减一1
我减一完毕了--------------------------唤醒其他线程加一0

Process finished with exit code 0

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值