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

  1. Guarded Suspension:一个线程需要等待另一个线程的执行结果

  2. 理解

  • 一个线程需要将结果传递给另一个线程,将这两个线程关联到到同一个 GuardedObject
  • 如果需要源源不断地传递结果,需要使用消息队列(生产者-消费者模型)
  • JDK 中,join 的实现、Future 的实现用的都是保护性暂停模式
  • 因为要等待,所以算作同步模式
  1. 实现
  • 创建两个线程和一个守护对象,线程 1 wait 等待下载结果 response,线程 2 执行下载,下载完成将结果赋值给 GuardedObject 的 response 属性,然后 notify 唤醒线程 1,线程 1 被唤醒,继续执行,两个线程通过 GuardedObject 传递要用的结果
  1. 优点
  • join 需要等一个线程运行结束了才能得到该线程的结果
  • join 等待结果的变量只能是全局的,GuardedObject 可以是局部的
  1. 扩展 1
  • 超时退出:wait(timeout) 只等待一段时间,如果超时了还没有获取到结果就直接退出循环

(resposne 是下载完成之后赋值的,response 不为空之后就可以退出循环,返回 res)

**6. join 的原理:**保护性暂停是等待一个线程的结果,join 是等待一个线程的结束

  • 就是应用了保护性暂停模式,如果**线程还存活(对应上面的下载结果 response == null)**且未超时就一直等待,如果超时了(还没有得到结果)就退出循环

    复制代码
      /**
       * Waits at most {@code millis} milliseconds for this thread to
       * die. A timeout of {@code 0} means to wait forever.
       *
       * <p> This implementation uses a loop of {@code this.wait} calls
       * conditioned on {@code this.isAlive}. As a thread terminates the
       * {@code this.notifyAll} method is invoked. It is recommended that
       * applications not use {@code wait}, {@code notify}, or
       * {@code notifyAll} on {@code Thread} instances.
       *
       * @param  millis
       *         the time to wait in milliseconds
       *
       * @throws  IllegalArgumentException
       *          if the value of {@code millis} is negative
       *
       * @throws  InterruptedException
       *          if any thread has interrupted the current thread. The
       *          <i>interrupted status</i> of the current thread is
       *          cleared when this exception is thrown.
       */
      public final synchronized void join(long millis)
      throws InterruptedException {
          long base = System.currentTimeMillis();
          long now = 0;
    
          if (millis < 0) {
              throw new IllegalArgumentException("timeout value is negative");
          }
    
          if (millis == 0) {
              while (isAlive()) {
                  wait(0);
              }
          } else {
              while (isAlive()) {
                  long delay = millis - now;
                  if (delay <= 0) {
                      break;
                  }
                  wait(delay);
                  now = System.currentTimeMillis() - base;
              }
          }
      }
  1. 扩展2:生产者消费者节耦
相关推荐
QT 小鲜肉几秒前
【个人成长笔记】将Try Ubuntu里面配置好的文件系统克隆在U盘上(创建一个带有持久化功能的Ubuntu Live USB系统)
linux·开发语言·数据库·笔记·ubuntu
Lisonseekpan几秒前
Git 命令大全:从基础到高级操作
java·git·后端·github·团队开发
Pointer Pursuit5 分钟前
C++——二叉搜索树
开发语言·c++
澪吟8 分钟前
C++ 从入门到进阶:核心知识与学习指南
开发语言·c++
CodeCraft Studio15 分钟前
国产化Excel处理控件Spire.XLS教程:使用Java将CSV转换为PDF(含格式设置)
java·pdf·excel·spire.xls·文档格式转换·csv转pdf
热爱编程的小白白27 分钟前
【Playwright自动化】安装和使用
开发语言·python
听风吟丶29 分钟前
Java NIO 深度解析:从 BIO 到 NIO 的演进与实战
开发语言·python
学历真的很重要29 分钟前
LangChain V1.0 Messages 详细指南
开发语言·后端·语言模型·面试·langchain·职场发展·langgraph
sali-tec32 分钟前
C# 基于halcon的视觉工作流-章58-输出点云图
开发语言·人工智能·算法·计算机视觉·c#
lpfasd12333 分钟前
Rust + WebAssembly:让嵌入式设备被浏览器调试
开发语言·rust·wasm