8.1 是什么

阻塞队列,顾名思义,首先它是一个队列,而一个阻塞队列在数据结构中所起的作用大致如下图所示:
image.png

  • 线程 1 往阻塞队列中添加元素,而线程 2 从阻塞队列中移除元素,当阻塞队列是空时,从队列中获取元素的操作将会被阻塞。
  • 当阻塞队列是满时,往队列里添加元素的操作将会被阻塞。
  • 试图从空的阻塞队列中获取元素的线程将会被阻塞,直到其他的线程往空的队列插入新的元素。同样试图往已满的阻塞队列中添加新元素的线程同样也会被阻塞,直到其他的线程从列中移除一个或者多个元素或者完全清空队列后使队列重新变得空闲起来并后续新增

    8.2 有什么好处

    在多线程领域:所谓阻塞,在某些情况下会挂起线程(即阻塞),一旦条件满足,被挂起的线程又会自动被唤醒
    BlockingQueue 好处是我们不需要关心什么时候需要阻塞线程,什么时候需要唤醒线程,因为这一切 BlockingQueue 都给你一手包办了
    在 concurrent 包发布以前,在多线程环境下,我们每个程序员都必须去自己控制这些细节,尤其还要兼顾效率和线程安全,而这会给我们的程序带来不小的复杂度。

    8.3 种类分析

    ArrayBlockingQueue:由数组结构组成的有界阴塞队列。
    LinkedBlockingQueue:由链表结构组成的有界(但大小默认值为 Integer.MAX_VALUE)阻塞队列。
    PriorityBlockingQueue:支持优先级排序的无界阻塞队列。
    DelayQueue:使用优先级队列实现的延迟无界阻塞队列。
    SynchronousQueue:不存储元素的阻塞队列,也即单个元素的队列。
    LinkedTransferQueue:由链表结构组成的无界阻塞队列。
    LinkedBlockingDeque:由链表结构组成的双向阻塞队列。

    8.4 核心方法

    | 方法类型 | 抛出异常 | 特殊值 | 阻塞 | 超时 | | —- | —- | —- | —- | —- | | 插入 | add(e) | offer(e) | put(e) | offer(e,time,unit) | | 移除 | remove() | poll() | take() | poll(time,unit) | | 检查 | element() | peek() | 不可用 | 不可用 |
抛出异常 当阻塞队列满时,再往队列里 add 插入元素会抛 IllegalStateException: Queue full
当阻塞队列空时,再往队列里 remove 移除元素会抛 NoSuchElementException
特殊值 插入方法,成功 ture 失败 false
移除方法,成功返回出队列的元素,队列里面没有就返回 null
一直阻塞 当阻塞队列满时,生产者线程继续往队列里 put 元素,队列会一直阻塞生产线程直到 put 数据 or 响应中断退出。
当阻塞队列空时,消费者线程试图从队列里 take 元素,队列会一直阻塞消费者线程直到队列可用。
超时退出 当阻塞队列满时,队列会阻塞生产者线程一定时间,超过后限时后生产者线程会退出

8.4.1 抛出异常 api

  1. package s02.e08;
  2. import java.util.concurrent.ArrayBlockingQueue;
  3. import java.util.concurrent.BlockingQueue;
  4. public class BlockingQueueDemo {
  5. public static void main(String[] args) throws Exception {
  6. BlockingQueue<String> blockingQueue = new ArrayBlockingQueue<>(3);
  7. System.out.println(blockingQueue.add("a"));
  8. System.out.println(blockingQueue.add("b"));
  9. System.out.println(blockingQueue.add("c"));
  10. System.out.println(blockingQueue.add("x"));
  11. }
  12. }

image.png

  1. package s02.e08;
  2. import java.util.concurrent.ArrayBlockingQueue;
  3. import java.util.concurrent.BlockingQueue;
  4. public class BlockingQueueDemo {
  5. public static void main(String[] args) throws Exception {
  6. BlockingQueue<String> blockingQueue = new ArrayBlockingQueue<>(3);
  7. System.out.println(blockingQueue.add("a"));
  8. System.out.println(blockingQueue.add("b"));
  9. System.out.println(blockingQueue.add("c"));
  10. blockingQueue.remove();
  11. blockingQueue.remove();
  12. blockingQueue.remove();
  13. blockingQueue.remove();
  14. }
  15. }

image.png

  1. package s02.e08;
  2. import java.util.concurrent.ArrayBlockingQueue;
  3. import java.util.concurrent.BlockingQueue;
  4. public class BlockingQueueDemo {
  5. public static void main(String[] args) throws Exception {
  6. BlockingQueue<String> blockingQueue = new ArrayBlockingQueue<>(3);
  7. System.out.println(blockingQueue.add("a"));
  8. System.out.println(blockingQueue.add("b"));
  9. System.out.println(blockingQueue.add("c"));
  10. System.out.println(blockingQueue.element());
  11. blockingQueue.remove();
  12. blockingQueue.remove();
  13. blockingQueue.remove();
  14. }
  15. }

image.png

8.4.2 特殊值 api

  1. package s02.e08;
  2. import java.util.concurrent.ArrayBlockingQueue;
  3. import java.util.concurrent.BlockingQueue;
  4. public class BlockingQueueDemo {
  5. public static void main(String[] args) throws Exception {
  6. BlockingQueue<String> blockingQueue = new ArrayBlockingQueue<>(3);
  7. System.out.println(blockingQueue.offer("a"));
  8. System.out.println(blockingQueue.offer("a"));
  9. System.out.println(blockingQueue.offer("a"));
  10. System.out.println(blockingQueue.offer("x"));
  11. }
  12. }

image.png

  1. package s02.e08;
  2. import java.util.concurrent.ArrayBlockingQueue;
  3. import java.util.concurrent.BlockingQueue;
  4. public class BlockingQueueDemo {
  5. public static void main(String[] args) throws Exception {
  6. BlockingQueue<String> blockingQueue = new ArrayBlockingQueue<>(3);
  7. System.out.println(blockingQueue.offer("a"));
  8. System.out.println(blockingQueue.offer("b"));
  9. System.out.println(blockingQueue.offer("c"));
  10. System.out.println(blockingQueue.offer("x"));
  11. System.out.println(blockingQueue.peek());
  12. }
  13. }

image.png

  1. package s02.e08;
  2. import java.util.concurrent.ArrayBlockingQueue;
  3. import java.util.concurrent.BlockingQueue;
  4. public class BlockingQueueDemo {
  5. public static void main(String[] args) throws Exception {
  6. BlockingQueue<String> blockingQueue = new ArrayBlockingQueue<>(3);
  7. System.out.println(blockingQueue.offer("a"));
  8. System.out.println(blockingQueue.offer("b"));
  9. System.out.println(blockingQueue.offer("c"));
  10. System.out.println(blockingQueue.offer("x"));
  11. System.out.println(blockingQueue.peek());
  12. System.out.println(blockingQueue.poll());
  13. System.out.println(blockingQueue.poll());
  14. System.out.println(blockingQueue.poll());
  15. System.out.println(blockingQueue.poll());
  16. }
  17. }

image.png

8.4.3 阻塞 api

  1. package s02.e08;
  2. import java.util.concurrent.ArrayBlockingQueue;
  3. import java.util.concurrent.BlockingQueue;
  4. public class BlockingQueueDemo {
  5. public static void main(String[] args) throws Exception {
  6. BlockingQueue<String> blockingQueue = new ArrayBlockingQueue<>(3);
  7. blockingQueue.put("a");
  8. blockingQueue.put("a");
  9. blockingQueue.put("a");
  10. System.out.println("=========================");
  11. blockingQueue.put("x");
  12. }
  13. }

image.png

  1. package s02.e08;
  2. import java.util.concurrent.ArrayBlockingQueue;
  3. import java.util.concurrent.BlockingQueue;
  4. public class BlockingQueueDemo {
  5. public static void main(String[] args) throws Exception {
  6. BlockingQueue<String> blockingQueue = new ArrayBlockingQueue<>(3);
  7. blockingQueue.put("a");
  8. blockingQueue.put("a");
  9. blockingQueue.put("a");
  10. System.out.println("=========================");
  11. blockingQueue.take();
  12. blockingQueue.take();
  13. blockingQueue.take();
  14. blockingQueue.take();
  15. }
  16. }

image.png

8.4.4 超时 api

  1. package s02.e08;
  2. import java.util.concurrent.ArrayBlockingQueue;
  3. import java.util.concurrent.BlockingQueue;
  4. import java.util.concurrent.TimeUnit;
  5. public class BlockingQueueDemo {
  6. public static void main(String[] args) throws Exception {
  7. BlockingQueue<String> blockingQueue = new ArrayBlockingQueue<>(3);
  8. System.out.println(blockingQueue.offer("a", 2L, TimeUnit.SECONDS));
  9. System.out.println(blockingQueue.offer("a", 2L, TimeUnit.SECONDS));
  10. System.out.println(blockingQueue.offer("a", 2L, TimeUnit.SECONDS));
  11. System.out.println(blockingQueue.offer("a", 2L, TimeUnit.SECONDS));
  12. }
  13. }

image.png
最后一个等了 2 秒才 false

8.5 SynchronousQueue

SynchronousQueue 没有容量。
与其他 BlockingQueue 不同,SynchronousQueue 是一个不存储元素的 BlockingQueue。
每一个 put 操作必须要等待一个 take 操作,否则不能继续添加元素,反之亦然。

  1. package s02.e08;
  2. import java.util.concurrent.BlockingQueue;
  3. import java.util.concurrent.SynchronousQueue;
  4. import java.util.concurrent.TimeUnit;
  5. public class SynchronousQueueDemo {
  6. public static void main(String[] args) throws InterruptedException {
  7. BlockingQueue<String> blockingQueue = new SynchronousQueue<>();
  8. new Thread(() -> {
  9. try {
  10. System.out.println(Thread.currentThread().getName() + "\t put 1");
  11. blockingQueue.put("1");
  12. System.out.println(Thread.currentThread().getName() + "\t put 2");
  13. blockingQueue.put("2");
  14. System.out.println(Thread.currentThread().getName() + "\t put 3");
  15. blockingQueue.put("3");
  16. } catch (InterruptedException e) {
  17. e.printStackTrace();
  18. }
  19. }, "AAA").start();
  20. new Thread(() -> {
  21. try {
  22. // 暂停一会儿线程
  23. try {
  24. TimeUnit.SECONDS.sleep(5);
  25. } catch (InterruptedException e) {
  26. e.printStackTrace();
  27. }
  28. System.out.println(Thread.currentThread().getName() + "\t" + blockingQueue.take());
  29. try {
  30. TimeUnit.SECONDS.sleep(5);
  31. } catch (InterruptedException e) {
  32. e.printStackTrace();
  33. }
  34. System.out.println(Thread.currentThread().getName() + "\t" + blockingQueue.take());
  35. try {
  36. TimeUnit.SECONDS.sleep(5);
  37. } catch (InterruptedException e) {
  38. e.printStackTrace();
  39. }
  40. System.out.println(Thread.currentThread().getName() + "\t" + blockingQueue.take());
  41. } catch (InterruptedException e) {
  42. e.printStackTrace();
  43. }
  44. }, "BBB").start();
  45. }
  46. }

image.png

8.6 用在哪里

  • 生产者消费者模式
  • 线程池
  • 消息中间件

    8.6.1 生产者消费者模式——传统版

    一个初始值为零的变量,两个线程对其交替操作,一个加 1 一个减 1 ,来 5 轮 ```java package s02.e08;

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

// 资源类 class ShareData { private int number = 0; private Lock lock = new ReentrantLock(); private Condition condition = lock.newCondition();

  1. public void increment() throws Exception {
  2. lock.lock();
  3. try {
  4. // 1 判断
  5. while (number != 0) {
  6. // 等待,不能生产
  7. condition.await();
  8. }
  9. // 2 干活
  10. number++;
  11. System.out.println(Thread.currentThread().getName() + "\t" + number);
  12. // 3 通知唤醒
  13. condition.signalAll();
  14. } catch (Exception e) {
  15. e.printStackTrace();
  16. } finally {
  17. lock.unlock();
  18. }
  19. }
  20. public void decrement() throws Exception {
  21. lock.lock();
  22. try {
  23. // 1 判断
  24. while (number == 0) {
  25. // 等待,不能生产
  26. condition.await();
  27. }
  28. // 2 干活
  29. number--;
  30. System.out.println(Thread.currentThread().getName() + "\t" + number);
  31. // 3 通知唤醒
  32. condition.signalAll();
  33. } catch (Exception e) {
  34. e.printStackTrace();
  35. } finally {
  36. lock.unlock();
  37. }
  38. }

}

public class ProdConsumer_TraditionDemo { public static void main(String[] args) { ShareData shareData = new ShareData();

  1. new Thread(() -> {
  2. for (int i = 1; i <= 5; i++) {
  3. try {
  4. shareData.increment();
  5. } catch (Exception e) {
  6. e.printStackTrace();
  7. }
  8. }
  9. }, "AA").start();
  10. new Thread(() -> {
  11. for (int i = 1; i <= 5; i++) {
  12. try {
  13. shareData.decrement();
  14. } catch (Exception e) {
  15. e.printStackTrace();
  16. }
  17. }
  18. }, "BB").start();
  19. }

}

  1. ![image.png](https://cdn.nlark.com/yuque/0/2022/png/390086/1644769968129-3644d405-d7c9-4ee2-af70-a840d903bb03.png#clientId=ube101e58-d1ee-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=216&id=u80484c9d&margin=%5Bobject%20Object%5D&name=image.png&originHeight=216&originWidth=69&originalType=binary&ratio=1&rotation=0&showTitle=false&size=1667&status=done&style=none&taskId=u24fadda7-1d29-48e1-8c75-03990b8396e&title=&width=69)<br />多线程的判断一定要用 while 判断,不能用 if 判断,否则会产生虚假唤醒<br />![image.png](https://cdn.nlark.com/yuque/0/2022/png/390086/1644770372605-773e4676-558f-454a-b0e0-263a86167c5e.png#clientId=ube101e58-d1ee-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=202&id=ucc0bf973&margin=%5Bobject%20Object%5D&name=image.png&originHeight=202&originWidth=753&originalType=binary&ratio=1&rotation=0&showTitle=false&size=79637&status=done&style=none&taskId=ue9c80a50-8059-47d5-9c38-23f81e7d6ef&title=&width=753)
  2. ```java
  3. package s02.e08;
  4. import java.util.concurrent.locks.Condition;
  5. import java.util.concurrent.locks.Lock;
  6. import java.util.concurrent.locks.ReentrantLock;
  7. // 资源类
  8. class ShareData {
  9. private int number = 0;
  10. private Lock lock = new ReentrantLock();
  11. private Condition condition = lock.newCondition();
  12. public void increment() throws Exception {
  13. lock.lock();
  14. try {
  15. // 1 判断
  16. if (number != 0) {
  17. // 等待,不能生产
  18. condition.await();
  19. }
  20. // 2 干活
  21. number++;
  22. System.out.println(Thread.currentThread().getName() + "\t" + number);
  23. // 3 通知唤醒
  24. condition.signalAll();
  25. } catch (Exception e) {
  26. e.printStackTrace();
  27. } finally {
  28. lock.unlock();
  29. }
  30. }
  31. public void decrement() throws Exception {
  32. lock.lock();
  33. try {
  34. // 1 判断
  35. if (number == 0) {
  36. // 等待,不能生产
  37. condition.await();
  38. }
  39. // 2 干活
  40. number--;
  41. System.out.println(Thread.currentThread().getName() + "\t" + number);
  42. // 3 通知唤醒
  43. condition.signalAll();
  44. } catch (Exception e) {
  45. e.printStackTrace();
  46. } finally {
  47. lock.unlock();
  48. }
  49. }
  50. }
  51. public class ProdConsumer_TraditionDemo {
  52. public static void main(String[] args) {
  53. ShareData shareData = new ShareData();
  54. new Thread(() -> {
  55. for (int i = 1; i <= 5; i++) {
  56. try {
  57. shareData.increment();
  58. } catch (Exception e) {
  59. e.printStackTrace();
  60. }
  61. }
  62. }, "AA").start();
  63. new Thread(() -> {
  64. for (int i = 1; i <= 5; i++) {
  65. try {
  66. shareData.decrement();
  67. } catch (Exception e) {
  68. e.printStackTrace();
  69. }
  70. }
  71. }, "BB").start();
  72. new Thread(() -> {
  73. for (int i = 1; i <= 5; i++) {
  74. try {
  75. shareData.increment();
  76. } catch (Exception e) {
  77. e.printStackTrace();
  78. }
  79. }
  80. }, "CC").start();
  81. new Thread(() -> {
  82. for (int i = 1; i <= 5; i++) {
  83. try {
  84. shareData.decrement();
  85. } catch (Exception e) {
  86. e.printStackTrace();
  87. }
  88. }
  89. }, "DD").start();
  90. }
  91. }

image.png
这里会有生产 2 个的情况

8.6.2 Synchronized 和 Lock 有什么区别

  1. 原始构成

synchronized 是关键字属于 JVM 层面
monitorenter(底层是通过 monitor 对象来完成,其实 wait/notify 等方法也依赖于 monitor 对象只有在同步块或方法中才能调 wait/notify 等方法)
monitorexit
Lock 是具体类(java.util.concurrent.Locks.Lock)是 api 层面的锁

  1. package s02.e08;
  2. import java.util.concurrent.locks.ReentrantLock;
  3. public class SyncAndReentrantLockDemo {
  4. public static void main(String[] args) {
  5. synchronized (new Object()) {
  6. }
  7. new ReentrantLock();
  8. }
  9. }

image.png
出现两个 monitorexit 是为了保证不会死锁

  1. 使用方法

synchronized 不需要用户去手动释放锁,当 synchronized 代码执行完后系统会自动让线程释放对锁的占用
ReentrantLock 则需要用户去手动释放锁若没有主动释放锁,就有可能导致出现死锁现象
需要 lock( ) 和 unlock() 方法配合 try/finally 语句块来完成

  1. 等待是否可中断

synchronized 不可中断,除非抛出异常或者正常运行完成
ReentrantLock 可中断

  • 设置超时方法 tryLock(Long timeout,TimeUnit unit)
  • LockInterruptibly() 放代码块中,调用 interrupt() 方法可中断
  1. 加锁是否公平

synchronized 非公平锁
ReentrantLock 两者都可以,默认非公平锁,构造方法可以传入 boolean 值,true 为公平锁,false 为非公平锁

  1. 锁绑定多个条件 condition

synchronized 没有
ReentrantLock 用来实现分组唤醒需要唤醒的线程们,可以精确唤醒,而不是像 synchronized 要么随机唤醒一个线程要么唤醒全部线程。
题目:多线程之间按顺序调用,实现 A->B->C 三个线程启动,要求如下
AA 打印 5 次,BB 打印 10 次,CC 打印 15 次,紧接着 AA 打印 5 次,BB 打印 10次,CC 打印 15 次,来10轮

  1. package s02.e08;
  2. import java.util.concurrent.locks.Condition;
  3. import java.util.concurrent.locks.Lock;
  4. import java.util.concurrent.locks.ReentrantLock;
  5. class ShareResource {
  6. private int number = 1; //A:1 B:2 c:3
  7. private Lock lock = new ReentrantLock();
  8. private Condition c1 = lock.newCondition();
  9. private Condition c2 = lock.newCondition();
  10. private Condition c3 = lock.newCondition();
  11. public void print5() {
  12. lock.lock();
  13. try {
  14. // 1. 判断
  15. while (number != 1) {
  16. c1.await();
  17. }
  18. // 2. 干活
  19. for (int i = 1; i <= 5; i++) {
  20. System.out.println(Thread.currentThread().getName() + "\t" + i);
  21. }
  22. // 3. 通知
  23. number = 2;
  24. c2.signal();
  25. } catch (Exception e) {
  26. e.printStackTrace();
  27. } finally {
  28. lock.unlock();
  29. }
  30. }
  31. public void print10() {
  32. lock.lock();
  33. try {
  34. // 1. 判断
  35. while (number != 2) {
  36. c2.await();
  37. }
  38. // 2. 干活
  39. for (int i = 1; i <= 10; i++) {
  40. System.out.println(Thread.currentThread().getName() + "\t" + i);
  41. }
  42. // 3. 通知
  43. number = 3;
  44. c3.signal();
  45. } catch (Exception e) {
  46. e.printStackTrace();
  47. } finally {
  48. lock.unlock();
  49. }
  50. }
  51. public void print15() {
  52. lock.lock();
  53. try {
  54. // 1. 判断
  55. while (number != 3) {
  56. c3.await();
  57. }
  58. // 2. 干活
  59. for (int i = 1; i <= 15; i++) {
  60. System.out.println(Thread.currentThread().getName() + "\t" + i);
  61. }
  62. // 3. 通知
  63. number = 1;
  64. c1.signal();
  65. } catch (Exception e) {
  66. e.printStackTrace();
  67. } finally {
  68. lock.unlock();
  69. }
  70. }
  71. }
  72. public class SyncAndReentrantLockDemo {
  73. public static void main(String[] args) {
  74. ShareResource shareResource = new ShareResource();
  75. new Thread(() -> {
  76. for (int i = 1; i <= 10; i++) {
  77. shareResource.print5();
  78. }
  79. }, "A").start();
  80. new Thread(() -> {
  81. for (int i = 1; i <= 10; i++) {
  82. shareResource.print10();
  83. }
  84. }, "B").start();
  85. new Thread(() -> {
  86. for (int i = 1; i <= 10; i++) {
  87. shareResource.print15();
  88. }
  89. }, "C").start();
  90. }
  91. }

image.png

8.6.3 线程通信之生产者消费者阻塞队列版

  1. package s02.e08;
  2. import java.util.concurrent.ArrayBlockingQueue;
  3. import java.util.concurrent.BlockingQueue;
  4. import java.util.concurrent.TimeUnit;
  5. import java.util.concurrent.atomic.AtomicInteger;
  6. class MyResource {
  7. private volatile boolean FLAG = true; // 默认开启,进行生产+消费
  8. private AtomicInteger atomicInteger = new AtomicInteger();
  9. BlockingQueue<String> blockingQueue = null;
  10. public MyResource(BlockingQueue<String> blockingQueue) {
  11. this.blockingQueue = blockingQueue;
  12. System.out.println(blockingQueue.getClass().getName());
  13. }
  14. public void myprod() throws Exception {
  15. String data = null;
  16. boolean retvalue;
  17. while (FLAG) {
  18. data = atomicInteger.incrementAndGet() + "";
  19. retvalue = blockingQueue.offer(data, 2L, TimeUnit.SECONDS);
  20. if (retvalue) {
  21. System.out.println(Thread.currentThread().getName() + "\t插入队列" + data + "成功");
  22. } else {
  23. System.out.println(Thread.currentThread().getName() + "\t插入队列" + data + "失败");
  24. }
  25. TimeUnit.SECONDS.sleep(1);
  26. }
  27. System.out.println(Thread.currentThread().getName() + "\t大老板叫停了,表示FLAG=false,生产动作结束");
  28. }
  29. public void myConsumer() throws Exception {
  30. String result = null;
  31. while (FLAG) {
  32. result = blockingQueue.poll(2L, TimeUnit.SECONDS);
  33. if (null == result || result.equalsIgnoreCase("")) {
  34. FLAG = false;
  35. System.out.println(Thread.currentThread().getName() + "\t超过2秒钟没有取到蛋糕,消费退出");
  36. System.out.println();
  37. System.out.println();
  38. return;
  39. }
  40. System.out.println(Thread.currentThread().getName() + "\t消费队列蛋糕" + result + "成功");
  41. }
  42. }
  43. public void stop() throws Exception {
  44. this.FLAG = false;
  45. }
  46. }
  47. /**
  48. * volatile/cAs/atomicInteger/BLockQueue/线程交互/原子引用
  49. */
  50. public class Prodconsumer_BlockQueueDemo {
  51. public static void main(String[] args) throws Exception {
  52. MyResource myResource = new MyResource(new ArrayBlockingQueue<>(10));
  53. new Thread(() -> {
  54. System.out.println(Thread.currentThread().getName() + "\t生产线程启动");
  55. try {
  56. myResource.myprod();
  57. } catch (Exception e) {
  58. e.printStackTrace();
  59. }
  60. }, "Prod").start();
  61. new Thread(() -> {
  62. System.out.println(Thread.currentThread().getName() + "\t 消费线程启动");
  63. System.out.println();
  64. System.out.println();
  65. try {
  66. myResource.myConsumer();
  67. System.out.println();
  68. System.out.println();
  69. } catch (Exception e) {
  70. e.printStackTrace();
  71. }
  72. }, "Consumer").start();
  73. // 暂停一会儿线程
  74. try {
  75. TimeUnit.SECONDS.sleep(5);
  76. } catch (InterruptedException e) {
  77. e.printStackTrace();
  78. }
  79. System.out.println();
  80. System.out.println();
  81. System.out.println();
  82. System.out.println("5秒钟时间到,大老板main线程叫停,活动结束");
  83. myResource.stop();
  84. }
  85. }

image.png