(五)共享模型之管程【wait notify 】
创始人
2024-03-13 12:33:39
0

一、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 实现

@Slf4j(topic = "c.Test20")
public class Test20 {public static void main(String[] args) throws InterruptedException {GuardedObject guardedObject = new GuardedObject();new Thread(()->{log.debug("等待结果");ArrayList list = (ArrayList) guardedObject.get();log.debug("结果大小:{}",list.size());},"t1").start();new Thread(()->{log.debug("执行下载");try {Thread.sleep(3000);} catch (InterruptedException e) {e.printStackTrace();}ArrayList list = new ArrayList<>();list.add(1);list.add(1);list.add(1);guardedObject.complete(list);},"t2").start();}
}class GuardedObject {// 结果private Object response;// 获取结果public Object get() {synchronized (this) {while (response == null){try {this.wait();} catch (InterruptedException e) {e.printStackTrace();}}return response;}}// 产生结果public void complete(Object response) {synchronized (this) {// 给结果成员变量赋值this.response = response;this.notifyAll();}}
}

3.3 带超时版 GuardedObject

@Slf4j(topic = "c.Test20")
public class Test20 {public static void main(String[] args) throws InterruptedException {GuardedObject guardedObject = new GuardedObject();new Thread(()->{log.debug("等待结果");ArrayList list = (ArrayList) guardedObject.get(2000);log.debug("结果是:{}",list);},"t1").start();new Thread(()->{log.debug("执行下载");try {Thread.sleep(3000);} catch (InterruptedException e) {e.printStackTrace();}ArrayList list = new ArrayList<>();list.add(1);list.add(1);list.add(1);guardedObject.complete(list);},"t2").start();}
}// 增加超时效果
class GuardedObject {// 结果private Object response;// 获取结果// timeout 表示要等待多久 2000public Object get(long timeout) {synchronized (this) {// 开始时间 15:00:00long begin = System.currentTimeMillis();// 经历的时间long passedTime = 0;while (response == null) {// 这一轮循环应该等待的时间long waitTime = timeout - passedTime;// 经历的时间超过了最大等待时间时,退出循环if (timeout - passedTime <= 0) {break;}try {this.wait(waitTime); // 虚假唤醒 15:00:01} catch (InterruptedException e) {e.printStackTrace();}// 求得经历时间passedTime = System.currentTimeMillis() - begin; // 15:00:02  1s}return response;}}// 产生结果public void complete(Object response) {synchronized (this) {// 给结果成员变量赋值this.response = response;this.notifyAll();}}
}

3.4 原理之 join

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

等价于

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

3.5 多任务版 GuardedObject

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

 

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

// 增加超时效果
class GuardedObject {// 标识 Guarded Objectprivate int id;public GuardedObject(int id) {this.id = id;}public int getId() {return id;}// 结果private Object response;// 获取结果// timeout 表示要等待多久 2000public Object get(long timeout) {synchronized (this) {// 开始时间 15:00:00long begin = System.currentTimeMillis();// 经历的时间long passedTime = 0;while (response == null) {// 这一轮循环应该等待的时间long waitTime = timeout - passedTime;// 经历的时间超过了最大等待时间时,退出循环if (timeout - passedTime <= 0) {break;}try {this.wait(waitTime); // 虚假唤醒 15:00:01} catch (InterruptedException e) {e.printStackTrace();}// 求得经历时间passedTime = System.currentTimeMillis() - begin; // 15:00:02  1s}return response;}}// 产生结果public void complete(Object response) {synchronized (this) {// 给结果成员变量赋值this.response = response;this.notifyAll();}}
}
(2)中间解耦类
class Mailboxes {private static Map boxes = new Hashtable<>();private static int id = 1;// 产生唯一 idprivate static synchronized int generateId() {return id++;}public static GuardedObject getGuardedObject(int id) {return boxes.remove(id);}public static GuardedObject createGuardedObject() {GuardedObject go = new GuardedObject(generateId());boxes.put(go.getId(), go);return go;}public static Set getIds() {return boxes.keySet();}
}
(3)业务相关类
@Slf4j(topic = "c.People")
class People extends Thread{@Overridepublic void run() {// 收信GuardedObject guardedObject = Mailboxes.createGuardedObject();log.debug("开始收信 id:{}", guardedObject.getId());Object mail = guardedObject.get(5000);log.debug("收到信 id:{}, 内容:{}", guardedObject.getId(), mail);}
}@Slf4j(topic = "c.Postman")
class Postman extends Thread {private int id;private String mail;public Postman(int id, String mail) {this.id = id;this.mail = mail;}@Overridepublic void run() {GuardedObject guardedObject = Mailboxes.getGuardedObject(id);log.debug("送信 id:{}, 内容:{}", id, mail);guardedObject.complete(mail);}
}

(4)测试

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

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

4.1 定义

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

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

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

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

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

 

4.2 实现

@Slf4j(topic = "c.Test21")
public class Test21 {public static void main(String[] args) {MessageQueue queue = new MessageQueue(2);for (int i = 0; i < 3; i++) {int id = i;new Thread(() -> {queue.put(new Message(id , "值"+id));}, "生产者" + i).start();}new Thread(() -> {while(true) {sleep(1);Message message = queue.take();}}, "消费者").start();}}// 消息队列类 , java 线程之间通信
@Slf4j(topic = "c.MessageQueue")
class MessageQueue {// 消息的队列集合private LinkedList list = new LinkedList<>();// 队列容量private int capcity;public MessageQueue(int capcity) {this.capcity = capcity;}// 获取消息public Message take() {// 检查队列是否为空synchronized (list) {while(list.isEmpty()) {try {log.debug("队列为空, 消费者线程等待");list.wait();} catch (InterruptedException e) {e.printStackTrace();}}// 从队列头部获取消息并返回Message message = list.removeFirst();log.debug("已消费消息 {}", message);list.notifyAll();return message;}}// 存入消息public void put(Message message) {synchronized (list) {// 检查对象是否已满while(list.size() == capcity) {try {log.debug("队列已满, 生产者线程等待");list.wait();} catch (InterruptedException e) {e.printStackTrace();}}// 将消息加入队列尾部list.addLast(message);log.debug("已生产消息 {}", message);list.notifyAll();}}
}final class Message {private int id;private Object value;public Message(int id, Object value) {this.id = id;this.value = value;}public int getId() {return id;}public Object getValue() {return value;}@Overridepublic String toString() {return "Message{" +"id=" + id +", value=" + value +'}';}
}

相关内容

热门资讯

AWSECS:访问外部网络时出... 如果您在AWS ECS中部署了应用程序,并且该应用程序需要访问外部网络,但是无法正常访问,可能是因为...
AWSElasticBeans... 在Dockerfile中手动配置nginx反向代理。例如,在Dockerfile中添加以下代码:FR...
银河麒麟V10SP1高级服务器... 银河麒麟高级服务器操作系统简介: 银河麒麟高级服务器操作系统V10是针对企业级关键业务...
北信源内网安全管理卸载 北信源内网安全管理是一款网络安全管理软件,主要用于保护内网安全。在日常使用过程中,卸载该软件是一种常...
AWR报告解读 WORKLOAD REPOSITORY PDB report (PDB snapshots) AW...
AWS管理控制台菜单和权限 要在AWS管理控制台中创建菜单和权限,您可以使用AWS Identity and Access Ma...
​ToDesk 远程工具安装及... 目录 前言 ToDesk 优势 ToDesk 下载安装 ToDesk 功能展示 文件传输 设备链接 ...
群晖外网访问终极解决方法:IP... 写在前面的话 受够了群晖的quickconnet的小水管了,急需一个新的解决方法&#x...
不能访问光猫的的管理页面 光猫是现代家庭宽带网络的重要组成部分,它可以提供高速稳定的网络连接。但是,有时候我们会遇到不能访问光...
Azure构建流程(Power... 这可能是由于配置错误导致的问题。请检查构建流程任务中的“发布构建制品”步骤,确保正确配置了“Arti...