(1)Owner 线程发现条件不满足,调用 wait 方法,即可进入 WaitSet 变为 WAITING 状态。
(2)BLOCKED 和WAITING 的线程都处于阻塞状态,不占用 CPU 时间片。
(3)BLOCKED 线程会在 Owner 线程释放锁时唤醒。
(4)WAITING 线程 会在 Owner 线程调用 notify 或 notifyAll 时唤醒,但唤醒后并不意味着立刻获得锁,仍需要进入 EntryList 重新竞争。
(1)obj.wait():让进入 Object 监视器的线程到 waitSet 等待
(2)obj.notify():在 Object 上正在 waitSet 等待的线程中挑一个唤醒
(3)obj.notifyAll():让 Object 上正在 waitSet 等待的线程中全部唤醒
它们都是线程之间进行协作的手段,都属于 Object 对象的方法。 必须获得此对象的锁 ,才能调用这几个方法
wait() 方法会释放对象的锁,进入 WaitSet 等待区,从而让其他线程就机会获取对象的锁。无限制等待,直到 notify 为止
wait(long n) 有时限的等待 , 到 n 毫秒后结束等待,或是被 notify
(1)sleep 是 Thread 方法,而 wait 是 Object 的方法
(2)sleep不需要强制和 synchronized 配合使用,但 wait 需要
(3)sleep在睡眠的同时不会释放锁,但 wait 在等待的时候会释放锁对象
(4)它们状态 TIMED_WAITING

即 Guarded Suspension,用在一个线程等待另一个线程的执行结果。
要点:
(1)有一个结果需要从一个线程传递到了另一个线程,让它们关联同一个 GuardedObject。
(2)如果有结果不断从一个线程到另一个线程那么可以使用消息队列(见生产者/消费者)。
(3)JDK 中,join 的实现、Future 的实现,采用的就是此模式。
(4)因为要等待另一方的结果,因此归类到同步模式。
- @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();
- }
- }
- }
- @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 表示要等待多久 2000
- public Object get(long timeout) {
- synchronized (this) {
- // 开始时间 15:00:00
- long 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();
- }
- }
- }
是调用者轮询检查线程 alive 状态
t1.join();等价于
synchronized (t1) { // 调用者线程进入 t1 的 waitSet 等待, 直到 t1 运行结束 while (t1.isAlive()) { t1.wait(0); } }
join 体现的是【保护性暂停】模式
图中 Futures 就好比居民楼一层的信箱(每个信箱有房间编号),左侧的 t0 , t2 , t4 就好比等待邮件的居民,右侧的 t1 , t3 , t5 就好比邮递员如果需要在多个类之间使用 GuardedObject 对象,作为参数传递不是很方便,因此设计一个用来解耦的中间类, 这样不仅能够解耦【结果等待者】和【结果生产者】,还能够同时支持多个任务的管理![]()
(1)新增 id 用来标识 Guarded Object
// 增加超时效果 class GuardedObject { // 标识 Guarded Object private int id; public GuardedObject(int id) { this.id = id; } public int getId() { return id; } // 结果 private Object response; // 获取结果 // timeout 表示要等待多久 2000 public Object get(long timeout) { synchronized (this) { // 开始时间 15:00:00 long 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 Mapboxes = new Hashtable<>(); private static int id = 1; // 产生唯一 id private 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 SetgetIds() { return boxes.keySet(); } }
(3)业务相关类
@Slf4j(topic = "c.People") class People extends Thread{ @Override public 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; } @Override public 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(); } } }
(1)与前面的保护性暂停中的 GuardObject 不同,不需要产生结果和消费结果的线程一一对应
(2)消费队列可以用来平衡生产和消费的线程资源
(3)生产者仅负责产生结果数据,不关系数据该如何处理,而消费者专心处理结果数据。
(4)消息队列是有容量限制的,满时不会再加入数据,空时不会再消耗数据
(5)JDK 中各种阻塞队列,采用的就是这种模式
- @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;
- }
-
- @Override
- public String toString() {
- return "Message{" +
- "id=" + id +
- ", value=" + value +
- '}';
- }
- }