JUC-多线程(2.线程间的通信)学习笔记

1. 前言-题目

  • 题目: 两个线程,可以操作初始值为零的一个变量,实现一个线程对该变量 +1 ,一个线程对该变量 -1 ,实现交替,进行 10 轮,变量初始值为0

2. 线程通信

  1. 生产者 + 消费者
  • 消费者来消费,需要通知生产者先生产
  • 生产者生产完毕,需要通知消费者消费
  1. 通知等待唤醒机制

3. 多线程模板(2)

1. 根据生产者 + 消费者的工作顺序得出

1. 判断(判断有无产品)
2. 干活(如果没有产品,则先生成 ; 如果已有产品,则先消费)
3. 通知(通知对方我已经干完了,该你了)

2. 口诀(2):判断 - 干活 - 通知

3. 模板

  • 以前言的题目为例
public class JUC02_communication {
    public static void main(String[] args) {
        NUM num = new NUM();

        new Thread(()->{
            for (int i = 0; i < 10; i++){
                try {
                    // 加法
                    num.increment();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"Thread_A").start();
        new Thread(()->{
            for (int i = 0; i < 10; i++){
                try {
                    // 减法
                    num.decrement();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"Thread_B").start();
    }
}
class NUM{
    private int number = 0;

    public synchronized void increment() throws InterruptedException {
        //2.1 判断
        if (number != 0){
            this.wait();
        }
        //2.2 干活
        number++;
        System.out.println(Thread.currentThread().getName() + "\t"+number);
        //2.3 通知(唤醒)
        this.notifyAll();
    }

    public synchronized void decrement() throws InterruptedException {
        //2.1 判断
        if (number == 0){
            this.wait();
        }
        //2.2 干活
        number--;
        System.out.println(Thread.currentThread().getName() + "\t"+number);
        //2.3 通知(唤醒)
        this.notifyAll();
    }
}

3. 多线程模板(3)

1. 上述代码的问题

  • 如果题目修改一下—— 两个线程执行加操作,两个线程执行减操作
  • 换成4个线程会导致错误,虚假唤醒

2. 原因

  • 在java多线程判断时,不能用if,程序出事出在了判断上面
  • 突然有一添加的线程进到if了,突然中断了交出控制权,没有进行验证,而是直接走下去了,加了两次,甚至多次

3. 解决办法

  • 解决虚假唤醒:查看API,java.lang.Object
    在这里插入图片描述
  • 中断和虚假唤醒是可能产生的,所以要用loop循环,if只判断一次,while是只要唤醒就要拉回来再判断一次。if换成while

4. 口诀(3):多线程交换中,必须防止多线程的虚假唤醒,也即线程中判断只能用 while

5. 修改后的代码

class NUM{
    private int number = 0;

    public synchronized void increment() throws InterruptedException {
        //2.1 判断
        while (number != 0){
            this.wait();
        }
        //2.2 干活
        number++;
        System.out.println(Thread.currentThread().getName() + "\t"+number);
        //2.3 通知(唤醒)
        this.notifyAll();
    }

    public synchronized void decrement() throws InterruptedException {
        //2.1 判断
        while (number == 0){
            this.wait();
        }
        //2.2 干活
        number--;
        System.out.println(Thread.currentThread().getName() + "\t"+number);
        //2.3 通知(唤醒)
        this.notifyAll();
    }
}

4. 线程通信中 synchronized 与 lock

1. synchronized

  • synchronized 实现线程通信时,是使用 wait - notify / notifyAll
  • 示意图
    在这里插入图片描述

2. lock

  • lock 使用的是 Condition 对象
    在这里插入图片描述
  • 示意图
    在这里插入图片描述

3. 模板(4)- lock

class NUM{
    private int number = 0;

    private Lock lock = new ReentrantLock();
    //使用 lock 对应的 condition 对象
    private Condition condition = lock.newCondition();


    public void increment() throws InterruptedException {

        // lock 模块
        // 上锁
        lock.lock();
        try {
            //2.1 判断
            while (number != 0){
                condition.await();
            }
            //2.2 干活
            number++;
            System.out.println(Thread.currentThread().getName() + "\t"+number);
            //2.3 通知(唤醒)
            condition.signalAll();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            // 开锁
            lock.unlock();
        }

    }

    public void decrement() throws InterruptedException {
        lock.lock();
        try {
            //2.1 判断
            while (number == 0){
                condition.await();
            }
            //2.2 干活
            number--;
            System.out.println(Thread.currentThread().getName() + "\t"+number);
            //2.3 通知(唤醒)
            condition.signalAll();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            // 开锁
            lock.unlock();
        }
    }
}

5. 线程间定制化调用通信(精准唤醒)

1. 题目

  • 多线程之间按顺序调用
  • 也就是 A 打印 1 次, B 打印 2 次, C 打印 3 次,一共进行 10 轮,且严格按照 A -> B -> C的顺序

2. 口诀(4):注意标志位的修改和定位

3. 代码

public class JUC03_CustomizedCommunication {
    public static void main(String[] args) {
        ShareResource shareResource = new ShareResource();
        new Thread(()->{shareResource.printA();},"Thread_A").start();
        new Thread(()->{shareResource.printB();},"Thread_BB").start();
        new Thread(()->{shareResource.printC();},"Thread_CCC").start();
    }
}
class ShareResource{
    //标志位 :A-0 ; B-1 ; C-2
    private int state = 0;
    private Lock lock = new ReentrantLock();
    // 因为有三个线程所以需要三个 condition 对象
    private Condition conditionA = lock.newCondition();
    private Condition conditionB = lock.newCondition();
    private Condition conditionC = lock.newCondition();

    public void printA() {
        print(0, conditionA, conditionB);
    }
    public void printB() {
        print(1, conditionB, conditionC);
    }
    public void printC() {
        print(2, conditionC, conditionA);
    }

    /**
     * @param currentState :该线程对应的 标志位
     * @param currentCondition : 当前线程 对应的 condition 对象
     * @param nextCondition : 当前线程的下一个线程 对应的 condition 对象
     */
    private void print(int currentState, Condition currentCondition, Condition nextCondition) {
        // 循环 10 轮
        for (int i = 0; i < 10; ) {
            //上锁
            lock.lock();
            try {
                while (state % 3 != currentState) {
                    currentCondition.await();
                }
                for (int j = 0; j < (state % 3 + 1); j++){
                    System.out.println(Thread.currentThread().getName() + " print " + (j+1));
                }
                state++;
                i++;
                nextCondition.signal();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
        }
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

yuan_404

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

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

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

打赏作者

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

抵扣说明:

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

余额充值