• (五)共享模型之管程【wait notify 】


    一、wait notify 

    1. 小故事 - 为什么需要 wait

    2. 原理之 wait / notify

    (1)Owner 线程发现条件不满足,调用 wait 方法,即可进入 WaitSet 变为 WAITING 状态。 

    (2)BLOCKED 和WAITING 的线程都处于阻塞状态,不占用 CPU 时间片

    (3)BLOCKED 线程会在 Owner 线程释放锁时唤醒。

    (4)WAITING 线程 会在 Owner 线程调用 notify 或 notifyAll 时唤醒,但唤醒后并不意味着立刻获得锁,仍需要进入 EntryList 重新竞争。

    3. API 介绍

    (1)obj.wait():让进入 Object 监视器的线程到 waitSet 等待

    (2)obj.notify():在 Object 上正在 waitSet 等待的线程中挑一个唤醒

    (3)obj.notifyAll():让 Object 上正在 waitSet 等待的线程中全部唤醒

    它们都是线程之间进行协作的手段,都属于 Object 对象的方法。 必须获得此对象的锁 ,才能调用这几个方法
    wait() 方法会释放对象的锁,进入 WaitSet 等待区,从而让其他线程就机会获取对象的锁。无限制等待,直到  notify 为止
    wait(long n) 有时限的等待 , n 毫秒后结束等待,或是被 notify

    二、wait notify 的正确姿势

    1. sleep(long n) 和 wait(long n) 的区别

    (1)sleep 是 Thread 方法,而 wait 是 Object 的方法

    (2)sleep不需要强制和 synchronized 配合使用,但 wait 需要

    (3)sleep在睡眠的同时不会释放锁,但 wait 在等待的时候会释放锁对象

    (4)它们状态 TIMED_WAITING

    2. 正确姿势

    3. 同步模式之保护性暂停

    3.1 定义

    即 Guarded Suspension,用在一个线程等待另一个线程的执行结果。

    要点:

    (1)有一个结果需要从一个线程传递到了另一个线程,让它们关联同一个 GuardedObject。

    (2)如果有结果不断从一个线程到另一个线程那么可以使用消息队列(见生产者/消费者)。

    (3)JDK 中,join 的实现、Future 的实现,采用的就是此模式。

    (4)因为要等待另一方的结果,因此归类到同步模式。

    3.2 实现

    1. @Slf4j(topic = "c.Test20")
    2. public class Test20 {
    3. public static void main(String[] args) throws InterruptedException {
    4. GuardedObject guardedObject = new GuardedObject();
    5. new Thread(()->{
    6. log.debug("等待结果");
    7. ArrayList list = (ArrayList) guardedObject.get();
    8. log.debug("结果大小:{}",list.size());
    9. },"t1").start();
    10. new Thread(()->{
    11. log.debug("执行下载");
    12. try {
    13. Thread.sleep(3000);
    14. } catch (InterruptedException e) {
    15. e.printStackTrace();
    16. }
    17. ArrayList list = new ArrayList<>();
    18. list.add(1);
    19. list.add(1);
    20. list.add(1);
    21. guardedObject.complete(list);
    22. },"t2").start();
    23. }
    24. }
    25. class GuardedObject {
    26. // 结果
    27. private Object response;
    28. // 获取结果
    29. public Object get() {
    30. synchronized (this) {
    31. while (response == null){
    32. try {
    33. this.wait();
    34. } catch (InterruptedException e) {
    35. e.printStackTrace();
    36. }
    37. }
    38. return response;
    39. }
    40. }
    41. // 产生结果
    42. public void complete(Object response) {
    43. synchronized (this) {
    44. // 给结果成员变量赋值
    45. this.response = response;
    46. this.notifyAll();
    47. }
    48. }
    49. }

    3.3 带超时版 GuardedObject

    1. @Slf4j(topic = "c.Test20")
    2. public class Test20 {
    3. public static void main(String[] args) throws InterruptedException {
    4. GuardedObject guardedObject = new GuardedObject();
    5. new Thread(()->{
    6. log.debug("等待结果");
    7. ArrayList list = (ArrayList) guardedObject.get(2000);
    8. log.debug("结果是:{}",list);
    9. },"t1").start();
    10. new Thread(()->{
    11. log.debug("执行下载");
    12. try {
    13. Thread.sleep(3000);
    14. } catch (InterruptedException e) {
    15. e.printStackTrace();
    16. }
    17. ArrayList list = new ArrayList<>();
    18. list.add(1);
    19. list.add(1);
    20. list.add(1);
    21. guardedObject.complete(list);
    22. },"t2").start();
    23. }
    24. }
    25. // 增加超时效果
    26. class GuardedObject {
    27. // 结果
    28. private Object response;
    29. // 获取结果
    30. // timeout 表示要等待多久 2000
    31. public Object get(long timeout) {
    32. synchronized (this) {
    33. // 开始时间 15:00:00
    34. long begin = System.currentTimeMillis();
    35. // 经历的时间
    36. long passedTime = 0;
    37. while (response == null) {
    38. // 这一轮循环应该等待的时间
    39. long waitTime = timeout - passedTime;
    40. // 经历的时间超过了最大等待时间时,退出循环
    41. if (timeout - passedTime <= 0) {
    42. break;
    43. }
    44. try {
    45. this.wait(waitTime); // 虚假唤醒 15:00:01
    46. } catch (InterruptedException e) {
    47. e.printStackTrace();
    48. }
    49. // 求得经历时间
    50. passedTime = System.currentTimeMillis() - begin; // 15:00:02 1s
    51. }
    52. return response;
    53. }
    54. }
    55. // 产生结果
    56. public void complete(Object response) {
    57. synchronized (this) {
    58. // 给结果成员变量赋值
    59. this.response = response;
    60. this.notifyAll();
    61. }
    62. }
    63. }

    3.4 原理之 join

    是调用者轮询检查线程 alive 状态
    t1.join();

    等价于

    1. synchronized (t1) {
    2. // 调用者线程进入 t1 的 waitSet 等待, 直到 t1 运行结束
    3. while (t1.isAlive()) {
    4. t1.wait(0);
    5. }
    6. }
    join 体现的是【保护性暂停】模式

    3.5 多任务版 GuardedObject

    图中 Futures 就好比居民楼一层的信箱(每个信箱有房间编号),左侧的 t0 t2 t4 就好比等待邮件的居民,右侧的 t1 t3 t5 就好比邮递员
    如果需要在多个类之间使用 GuardedObject 对象,作为参数传递不是很方便,因此设计一个用来解耦的中间类, 这样不仅能够解耦【结果等待者】和【结果生产者】,还能够同时支持多个任务的管理

     

    (1)新增 id 用来标识 Guarded Object

    1. // 增加超时效果
    2. class GuardedObject {
    3. // 标识 Guarded Object
    4. private int id;
    5. public GuardedObject(int id) {
    6. this.id = id;
    7. }
    8. public int getId() {
    9. return id;
    10. }
    11. // 结果
    12. private Object response;
    13. // 获取结果
    14. // timeout 表示要等待多久 2000
    15. public Object get(long timeout) {
    16. synchronized (this) {
    17. // 开始时间 15:00:00
    18. long begin = System.currentTimeMillis();
    19. // 经历的时间
    20. long passedTime = 0;
    21. while (response == null) {
    22. // 这一轮循环应该等待的时间
    23. long waitTime = timeout - passedTime;
    24. // 经历的时间超过了最大等待时间时,退出循环
    25. if (timeout - passedTime <= 0) {
    26. break;
    27. }
    28. try {
    29. this.wait(waitTime); // 虚假唤醒 15:00:01
    30. } catch (InterruptedException e) {
    31. e.printStackTrace();
    32. }
    33. // 求得经历时间
    34. passedTime = System.currentTimeMillis() - begin; // 15:00:02 1s
    35. }
    36. return response;
    37. }
    38. }
    39. // 产生结果
    40. public void complete(Object response) {
    41. synchronized (this) {
    42. // 给结果成员变量赋值
    43. this.response = response;
    44. this.notifyAll();
    45. }
    46. }
    47. }
    (2)中间解耦类
    1. class Mailboxes {
    2. private static Map boxes = new Hashtable<>();
    3. private static int id = 1;
    4. // 产生唯一 id
    5. private static synchronized int generateId() {
    6. return id++;
    7. }
    8. public static GuardedObject getGuardedObject(int id) {
    9. return boxes.remove(id);
    10. }
    11. public static GuardedObject createGuardedObject() {
    12. GuardedObject go = new GuardedObject(generateId());
    13. boxes.put(go.getId(), go);
    14. return go;
    15. }
    16. public static Set getIds() {
    17. return boxes.keySet();
    18. }
    19. }
    (3)业务相关类
    1. @Slf4j(topic = "c.People")
    2. class People extends Thread{
    3. @Override
    4. public void run() {
    5. // 收信
    6. GuardedObject guardedObject = Mailboxes.createGuardedObject();
    7. log.debug("开始收信 id:{}", guardedObject.getId());
    8. Object mail = guardedObject.get(5000);
    9. log.debug("收到信 id:{}, 内容:{}", guardedObject.getId(), mail);
    10. }
    11. }
    12. @Slf4j(topic = "c.Postman")
    13. class Postman extends Thread {
    14. private int id;
    15. private String mail;
    16. public Postman(int id, String mail) {
    17. this.id = id;
    18. this.mail = mail;
    19. }
    20. @Override
    21. public void run() {
    22. GuardedObject guardedObject = Mailboxes.getGuardedObject(id);
    23. log.debug("送信 id:{}, 内容:{}", id, mail);
    24. guardedObject.complete(mail);
    25. }
    26. }

    (4)测试

    1. @Slf4j(topic = "c.Test20")
    2. public class Test20 {
    3. public static void main(String[] args) throws InterruptedException {
    4. for (int i = 0; i < 3; i++) {
    5. new People().start();
    6. }
    7. Sleeper.sleep(1);
    8. for (Integer id : Mailboxes.getIds()) {
    9. new Postman(id, "内容" + id).start();
    10. }
    11. }
    12. }

    4. 异步模式之生产者消费者

    4.1 定义

    (1)与前面的保护性暂停中的 GuardObject 不同,不需要产生结果和消费结果的线程一一对应

    (2)消费队列可以用来平衡生产和消费的线程资源

    (3)生产者仅负责产生结果数据,不关系数据该如何处理,而消费者专心处理结果数据。

    (4)消息队列是有容量限制的,满时不会再加入数据,空时不会再消耗数据

    (5)JDK 中各种阻塞队列,采用的就是这种模式

     

    4.2 实现

    1. @Slf4j(topic = "c.Test21")
    2. public class Test21 {
    3. public static void main(String[] args) {
    4. MessageQueue queue = new MessageQueue(2);
    5. for (int i = 0; i < 3; i++) {
    6. int id = i;
    7. new Thread(() -> {
    8. queue.put(new Message(id , "值"+id));
    9. }, "生产者" + i).start();
    10. }
    11. new Thread(() -> {
    12. while(true) {
    13. sleep(1);
    14. Message message = queue.take();
    15. }
    16. }, "消费者").start();
    17. }
    18. }
    19. // 消息队列类 , java 线程之间通信
    20. @Slf4j(topic = "c.MessageQueue")
    21. class MessageQueue {
    22. // 消息的队列集合
    23. private LinkedList list = new LinkedList<>();
    24. // 队列容量
    25. private int capcity;
    26. public MessageQueue(int capcity) {
    27. this.capcity = capcity;
    28. }
    29. // 获取消息
    30. public Message take() {
    31. // 检查队列是否为空
    32. synchronized (list) {
    33. while(list.isEmpty()) {
    34. try {
    35. log.debug("队列为空, 消费者线程等待");
    36. list.wait();
    37. } catch (InterruptedException e) {
    38. e.printStackTrace();
    39. }
    40. }
    41. // 从队列头部获取消息并返回
    42. Message message = list.removeFirst();
    43. log.debug("已消费消息 {}", message);
    44. list.notifyAll();
    45. return message;
    46. }
    47. }
    48. // 存入消息
    49. public void put(Message message) {
    50. synchronized (list) {
    51. // 检查对象是否已满
    52. while(list.size() == capcity) {
    53. try {
    54. log.debug("队列已满, 生产者线程等待");
    55. list.wait();
    56. } catch (InterruptedException e) {
    57. e.printStackTrace();
    58. }
    59. }
    60. // 将消息加入队列尾部
    61. list.addLast(message);
    62. log.debug("已生产消息 {}", message);
    63. list.notifyAll();
    64. }
    65. }
    66. }
    67. final class Message {
    68. private int id;
    69. private Object value;
    70. public Message(int id, Object value) {
    71. this.id = id;
    72. this.value = value;
    73. }
    74. public int getId() {
    75. return id;
    76. }
    77. public Object getValue() {
    78. return value;
    79. }
    80. @Override
    81. public String toString() {
    82. return "Message{" +
    83. "id=" + id +
    84. ", value=" + value +
    85. '}';
    86. }
    87. }

  • 相关阅读:
    7. 通配符和正则表达式
    字符编码转换时发生内存越界引发的摄像头切换失败问题的排查
    51单片机应用从零开始(二)
    STM32实战总结:HAL之RTC
    C++ 数组
    MySQL基础
    直方图均衡化(三,c#实现)
    你好,以太坊社区,你准备好参加 ETH India 2022 黑客马拉松活动了吗
    npm install报错 缺少python
    es基础学习笔记问题总结
  • 原文地址:https://blog.csdn.net/yirenyuan/article/details/128129993