27.Java 线程间通信(synchronized 实现线程间通信、Lock 实现线程间通信)

一、线程间通信

1、概述
  • 线程间通信的模型有两种:共享内存和消息传递
2、多线程编程步骤(中)
  1. 创建资源类,在资源类中创建属性和操作方法

  2. 在资源类操作方法进行判断、操作、通知

  3. 创建多个线程,调用资源类中的操作方法


二、synchronized 实现线程间通信

1、需求
  • 通过使用两个线程对值(0)进行操作,一个线程加 1,一个线程减 1,交替实现多次
2、具体实现
(1)资源类
  • Share 类
java 复制代码
package com.my.communicate;

public class Share {

    // 初始值
    private int number;

    // 加 1 的方法
    public synchronized void increase() throws InterruptedException {

        // number 为 0 则等待
        if (number != 0) {
            this.wait();
        }
        number++;
        System.out.println(Thread.currentThread().getName() + "  " + number);

        // 通知其他线程
        this.notifyAll();
    }

    // 减 1 的方法
    public synchronized void decrease() throws InterruptedException {

        // number 为 0 则等待
        if (number != 1) {
            this.wait();
        }
        number--;
        System.out.println(Thread.currentThread().getName() + "  " + number);

        // 通知其他线程
        this.notifyAll();
    }
}
(2)多线程测试
  • ShareTest 类
java 复制代码
package com.my.communicate;

public class ShareTest {
    public static void main(String[] args) {
        Share share = new Share();

        Thread thread1 = new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                try {
                    share.increase();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }, "AA");
        Thread thread2 = new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                try {
                    share.decrease();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }, "BB");

        thread1.start();
        thread2.start();
    }
}
3、虚假唤醒
  • 在当前 synchronized 实现线程间通信案例中再增加两个线程,执行结果会不符合预期,根本原因是虚假唤醒问题

  • 如果一个线程执行完毕后,通知其他线程,该线程又进入等待睡眠,被唤醒后,if 语句不会进行判断

  • 需要将 if 语句换成 while 语句,因为在哪里等待睡眠就会在哪里被唤醒,需要使用 while 语句再次进行判断

4、改进
(1)资源类
  • ShareImprove 类
java 复制代码
package com.my.communicate;

public class ShareImprove {

    // 初始值
    private int number;

    // 加 1 的方法
    public synchronized void increase() throws InterruptedException {

        // number 为 0 则等待
        while (number != 0) {
            this.wait();
        }
        number++;
        System.out.println(Thread.currentThread().getName() + "  " + number);

        // 通知其他线程
        this.notifyAll();
    }

    // 减 1 的方法
    public synchronized void decrease() throws InterruptedException {

        // number 为 0 则等待
        while (number != 1) {
            this.wait();
        }
        number--;
        System.out.println(Thread.currentThread().getName() + "  " + number);

        // 通知其他线程
        this.notifyAll();
    }
}
(2)多线程测试
  • ShareTestImprove 类
java 复制代码
package com.my.communicate;

public class ShareTestImprove {
    public static void main(String[] args) {
        Share share = new Share();

        Thread thread1 = new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                try {
                    share.increase();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }, "AA");
        Thread thread2 = new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                try {
                    share.decrease();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }, "BB");
        Thread thread3 = new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                try {
                    share.increase();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }, "CC");
        Thread thread4 = new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                try {
                    share.decrease();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }, "DD");

        thread1.start();
        thread2.start();
        thread3.start();
        thread4.start();
    }
}

三、Lock 实现线程间通信

1、需求
  • 通过使用两个线程对值(0)进行操作,一个线程加 1,一个线程减 1,交替实现多次
2、具体实现
  • LShare 类
(1)资源类
java 复制代码
package com.my.communicate;

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

public class LShare {

    // 初始值
    private int number;

    // 创建可重入锁
    private Lock lock;

    // 创建 Condition 对象
    private Condition condition;

    public LShare() {
        number = 0;
        lock = new ReentrantLock();
        condition = lock.newCondition();
    }

    // 加 1 的方法
    public void increase() {
        lock.lock();
        try {
            while (number != 0) {

                // 等待
                condition.await();
            }
            number++;
            System.out.println(Thread.currentThread().getName() + "  " + number);

            // 通知其他线程
            condition.signalAll();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }

    // 减 1 的方法
    public void decrease() {
        lock.lock();
        try {
            while (number != 1) {

                // 等待
                condition.await();
            }
            number--;
            System.out.println(Thread.currentThread().getName() + "  " + number);

            // 通知其他线程
            condition.signalAll();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }
}
(2)多线程测试
  • LShareTest 类
java 复制代码
package com.my.communicate;

public class LShareTest {
    public static void main(String[] args) {
        LShare lShare = new LShare();

        Thread thread1 = new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                lShare.increase();
            }
        }, "AA");
        Thread thread2 = new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                lShare.decrease();
            }
        }, "BB");
        Thread thread3 = new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                lShare.increase();
            }
        }, "CC");
        Thread thread4 = new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                lShare.decrease();
            }
        }, "DD");

        thread1.start();
        thread2.start();
        thread3.start();
        thread4.start();
    }
}
相关推荐
明月看潮生9 分钟前
青少年编程与数学 02-019 Rust 编程基础 08课题、字面量、运算符和表达式
开发语言·青少年编程·rust·编程与数学
huohuopro15 分钟前
thinkphp模板文件缺失没有报错/thinkphp无法正常访问控制器
后端·thinkphp
什码情况41 分钟前
星际篮球争霸赛/MVP争夺战 - 华为OD机试真题(A卷、Java题解)
java·数据结构·算法·华为od·面试·机试
天天打码44 分钟前
Rspack:字节跳动自研 Web 构建工具-基于 Rust打造高性能前端工具链
开发语言·前端·javascript·rust·开源
Petrichorzncu44 分钟前
Lua再学习
开发语言·学习·lua
AA-代码批发V哥1 小时前
正则表达式: 从基础到进阶的语法指南
java·开发语言·javascript·python·正则表达式
字节高级特工1 小时前
【C++】”如虎添翼“:模板初阶
java·c语言·前端·javascript·c++·学习·算法
晴天下小雨o1 小时前
排序算法总结
java·算法·排序算法
曼岛_1 小时前
[Java实战]Spring Boot 整合 Redis(十八)
java·spring boot·redis
向哆哆1 小时前
Netty在Java网络编程中的应用:实现高性能的异步通信
java·网络·php