|
| 1 | +# 深入理解线程通信 |
| 2 | + |
| 3 | +## 前言 |
| 4 | + |
| 5 | +开发中不免会遇到需要所有子线程执行完毕通知主线程处理某些逻辑的场景。 |
| 6 | + |
| 7 | +或者是线程 A 在执行到某个条件通知线程 B 执行某个操作。 |
| 8 | + |
| 9 | +可以通过以下几种方式实现: |
| 10 | + |
| 11 | + |
| 12 | +## 等待通知机制 |
| 13 | +> 等待通知模式是 Java 中比较经典的线程通信方式。 |
| 14 | +
|
| 15 | +两个线程通过对同一对象调用等待 wait() 和通知 notify() 方法来进行通讯。 |
| 16 | + |
| 17 | +如两个线程交替打印奇偶数: |
| 18 | + |
| 19 | +```java |
| 20 | +public class TwoThreadWaitNotify { |
| 21 | + |
| 22 | + private int start = 1; |
| 23 | + |
| 24 | + private boolean flag = false; |
| 25 | + |
| 26 | + public static void main(String[] args) { |
| 27 | + TwoThreadWaitNotify twoThread = new TwoThreadWaitNotify(); |
| 28 | + |
| 29 | + Thread t1 = new Thread(new OuNum(twoThread)); |
| 30 | + t1.setName("A"); |
| 31 | + |
| 32 | + |
| 33 | + Thread t2 = new Thread(new JiNum(twoThread)); |
| 34 | + t2.setName("B"); |
| 35 | + |
| 36 | + t1.start(); |
| 37 | + t2.start(); |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * 偶数线程 |
| 42 | + */ |
| 43 | + public static class OuNum implements Runnable { |
| 44 | + private TwoThreadWaitNotify number; |
| 45 | + |
| 46 | + public OuNum(TwoThreadWaitNotify number) { |
| 47 | + this.number = number; |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + public void run() { |
| 52 | + |
| 53 | + while (number.start <= 100) { |
| 54 | + synchronized (TwoThreadWaitNotify.class) { |
| 55 | + System.out.println("偶数线程抢到锁了"); |
| 56 | + if (number.flag) { |
| 57 | + System.out.println(Thread.currentThread().getName() + "+-+偶数" + number.start); |
| 58 | + number.start++; |
| 59 | + |
| 60 | + number.flag = false; |
| 61 | + TwoThreadWaitNotify.class.notify(); |
| 62 | + |
| 63 | + }else { |
| 64 | + try { |
| 65 | + TwoThreadWaitNotify.class.wait(); |
| 66 | + } catch (InterruptedException e) { |
| 67 | + e.printStackTrace(); |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + |
| 77 | + /** |
| 78 | + * 奇数线程 |
| 79 | + */ |
| 80 | + public static class JiNum implements Runnable { |
| 81 | + private TwoThreadWaitNotify number; |
| 82 | + |
| 83 | + public JiNum(TwoThreadWaitNotify number) { |
| 84 | + this.number = number; |
| 85 | + } |
| 86 | + |
| 87 | + @Override |
| 88 | + public void run() { |
| 89 | + while (number.start <= 100) { |
| 90 | + synchronized (TwoThreadWaitNotify.class) { |
| 91 | + System.out.println("奇数线程抢到锁了"); |
| 92 | + if (!number.flag) { |
| 93 | + System.out.println(Thread.currentThread().getName() + "+-+奇数" + number.start); |
| 94 | + number.start++; |
| 95 | + |
| 96 | + number.flag = true; |
| 97 | + |
| 98 | + TwoThreadWaitNotify.class.notify(); |
| 99 | + }else { |
| 100 | + try { |
| 101 | + TwoThreadWaitNotify.class.wait(); |
| 102 | + } catch (InterruptedException e) { |
| 103 | + e.printStackTrace(); |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | +} |
| 111 | +``` |
| 112 | + |
| 113 | +这里的线程 A 和线程 B 都对同一个对象 `TwoThreadWaitNotify.class` 获取锁,A 线程调用了同步对象的 wait() 方法释放了锁并进入 `WAITING` 状态。 |
| 114 | + |
| 115 | +B 线程调用了 notify() 方法,这样 A 线程收到通知之后就可以从 wait() 方法中返回。利用了 `TwoThreadWaitNotify.class` 对象完成了交互。 |
| 116 | + |
| 117 | +这里有一些注意点: |
| 118 | + |
| 119 | +- wait() 、nofify() 、nofityAll() 调用的前提都是获得了对象的锁(也可成为对象监视器)。 |
| 120 | +- 调用 wait() 方法后线程会释放锁,进入 `WAITING` 状态,该线程也会被移动到**等待队列**中。 |
| 121 | +- 调用 notify() 方法会将**等待队列**中的线程移动到**同步队列**中,线程状态也会更新为 `BLOCKED` |
| 122 | +- 从 wait() 方法返回的前提是调用 notify() 方法的线程释放锁,wait() 方法的线程获得锁。 |
| 123 | + |
| 124 | +等待通知有着一个经典范式: |
| 125 | + |
| 126 | +线程 A 作为消费者: |
| 127 | + |
| 128 | +1. 获取对象的锁。 |
| 129 | +2. 进入 while(判断条件),并调用 wait() 方法。 |
| 130 | +3. 当条件满足跳出循环执行具体处理逻辑。 |
| 131 | + |
| 132 | +线程 B 作为生产者: |
| 133 | + |
| 134 | +1. 获取对象锁。 |
| 135 | +2. 更改与线程 A 共用的判断条件。 |
| 136 | +3. 调用 notify() 方法。 |
| 137 | + |
| 138 | +伪代码如下: |
| 139 | + |
| 140 | +``` |
| 141 | +//Thread A |
| 142 | +
|
| 143 | +synchronized(Object){ |
| 144 | + while(条件){ |
| 145 | + Object.wait(); |
| 146 | + } |
| 147 | + //do something |
| 148 | +} |
| 149 | +
|
| 150 | +//Thread B |
| 151 | +synchronized(Object){ |
| 152 | + 条件=false;//改变条件 |
| 153 | + Object.notify(); |
| 154 | +} |
| 155 | +
|
| 156 | +``` |
| 157 | + |
| 158 | + |
| 159 | +## join() 方法 |
| 160 | + |
| 161 | +## volatile 共享内存 |
| 162 | + |
| 163 | +## CountDownLatch 并发工具 |
| 164 | + |
| 165 | +## 线程响应中断 |
| 166 | + |
| 167 | +## 线程池 awaitTermination() 方法 |
| 168 | + |
| 169 | +## 管道通信 |
0 commit comments