volatile 、wait 和 notify
- 🌲volatile关键字
 - 🚩保证内存可见性
 - 🚩volatile 不保证原⼦性
 
- 🌳wait 和 notify方法
 - 🚩wait()
 - 🚩notify()
 - 🚩notifyAll()方法
 
- ⭕wait 和 sleep 的对比( 面试题)
 
🌲volatile关键字
🚩保证内存可见性
volatile 修饰的变量, 能够保证 “内存可⻅性”.
 
 代码在写⼊ volatile 修饰的变量的时候,
• 改变线程⼯作内存中volatile变量副本的值
 • 将改变后的副本的值从⼯作内存刷新到主内存
代码在读取 volatile 修饰的变量的时候,
• 从主内存中读取volatile变量的最新值到线程的⼯作内存中
 • 从⼯作内存中读取volatile变量的副本
前⾯我们讨论内存可⻅性时说了, 直接访问⼯作内存(实际是 CPU 的寄存器或者 CPU 的缓存), 速度⾮
常快, 但是可能出现数据不⼀致的情况.
加上 volatile , 强制读写内存. 速度是慢了, 但是数据变的更准确了.
代码示例
在这个代码中
 • 创建两个线程 t1 和 t2
 • t1 中包含⼀个循环, 这个循环以 flag == 0 为循环条件.
 • t2 中从键盘读⼊⼀个整数, 并把这个整数赋值给 flag.
 • 预期当⽤⼾输⼊⾮ 0 的值的时候, t1 线程结束.
static class Counter {
 public int flag = 0;
}
public static void main(String[] args) {
 Counter counter = new Counter();
 Thread t1 = new Thread(() -> {
 while (counter.flag == 0) {
 // do nothing
 }
 System.out.println("循环结束!");
 });
 Thread t2 = new Thread(() -> {
 Scanner scanner = new Scanner(System.in);
 System.out.println("输⼊⼀个整数:");
 counter.flag = scanner.nextInt();
 });
 t1.start();
 t2.start();
}
// 执⾏效果
// 当⽤⼾输⼊⾮0值时, t1 线程循环不会结束. (这显然是⼀个 bug)
 
t1 读的是⾃⼰⼯作内存中的内容.
当 t2 对 flag 变量进⾏修改, 此时 t1 感知不到 flag 的变化.
如果给 flag 加上 volatile
static class Counter {
 public volatile int flag = 0;
}
// 执⾏效果
// 当⽤⼾输⼊⾮0值时, t1 线程循环能够⽴即结束.
 
🚩volatile 不保证原⼦性
volatile 和 synchronized 有着本质的区别. synchronized 能够保证原⼦性, volatile 保证的是内存可⻅
 性.
代码⽰例
 这个是最初的演⽰线程安全的代码.
 • 给 increase ⽅法去掉 synchronized
 • 给 count 加上 volatile 关键字.
static class Counter {
 volatile public int count = 0;
 void increase() {
 count++;
 }
}
public static void main(String[] args) throws InterruptedException {
 final Counter counter = new Counter();
 Thread t1 = new Thread(() -> {
 for (int i = 0; i < 50000; i++) {
 counter.increase();
 }
 });
 Thread t2 = new Thread(() -> {
 for (int i = 0; i < 50000; i++) {
 counter.increase();
 }
 });
 t1.start();
 t2.start();
 t1.join();
 t2.join();
 System.out.println(counter.count);
}
 
此时可以看到, 最终 count 的值仍然⽆法保证是 100000.
🌳wait 和 notify方法
由于线程之间是抢占式执⾏的, 因此线程之间执⾏的先后顺序难以预知.
 但是实际开发中有时候我们希望合理的协调多个线程之间的执⾏先后顺序.
 
 球场上的每个运动员都是独⽴的 “执⾏流” , 可以认为是⼀个 “线程”.
 ⽽完成⼀个具体的进攻得分动作, 则需要多个运动员相互配合, 按照⼀定的顺序执⾏⼀定的动作, 线程
 1 先 “传球” , 线程2 才能 “扣篮”.
完成这个协调⼯作, 主要涉及到三个⽅法
• wait() / wait(long timeout): 让当前线程进⼊等待状态.
• notify() / notifyAll(): 唤醒在当前对象上等待的线程
注意: wait, notify, notifyAll 都是 Object 类的⽅法
🚩wait()
wait 做的事情:
 • 使当前执⾏代码的线程进⾏等待. (把线程放到等待队列中)
 • 释放当前的锁
 • 满⾜⼀定条件时被唤醒, 重新尝试获取这个锁.
wait 要搭配 synchronized 来使⽤. 脱离 synchronized 使⽤ wait 会直接抛出异常.
wait 结束等待的条件:
 • 其他线程调⽤该对象的 notify ⽅法.
 • wait 等待时间超时 (wait ⽅法提供⼀个带有 timeout 参数的版本, 来指定等待时间).
 • 其他线程调⽤该等待线程的 interrupted ⽅法, 导致 wait 抛出 InterruptedException 异常.
代码⽰例: 观察wait()⽅法使⽤
public static void main(String[] args) throws InterruptedException {
 Object object = new Object();
 synchronized (object) {
 System.out.println("等待中");
 object.wait();
 System.out.println("等待结束");
 }
}
 
这样在执⾏到object.wait()之后就⼀直等待下去,那么程序肯定不能⼀直这么等待下去了。这个时候就
 需要使⽤到了另外⼀个⽅法唤醒的⽅法notify()。
🚩notify()
notify ⽅法是唤醒等待的线程.
• ⽅法notify()也要在同步⽅法或同步块中调⽤,该⽅法是⽤来通知那些可能等待该对象的对象锁的其
 它线程,对其发出通知notify,并使它们重新获取该对象的对象锁。
 • 如果有多个线程等待,则有线程调度器随机挑选出⼀个呈 wait 状态的线程。(并没有 “先来后到”)
 • 在notify()⽅法后,当前线程不会⻢上释放该对象锁,要等到执⾏notify()⽅法的线程将程序执⾏
 完,也就是退出同步代码块之后才会释放对象锁。
代码⽰例: 使⽤notify()⽅法唤醒线程
• 创建 WaitTask 类, 对应⼀个线程, run 内部循环调⽤ wait.
 • 创建 NotifyTask 类, 对应另⼀个线程, 在 run 内部调⽤⼀次 notify
 • 注意, WaitTask 和 NotifyTask 内部持有同⼀个 Object locker. WaitTask 和 NotifyTask 要想配合就
 需要搭配同⼀个 Object.
static class WaitTask implements Runnable {
 private Object locker;
 public WaitTask(Object locker) {
 this.locker = locker;
 }
 @Override
 public void run() {
 synchronized (locker) {
 while (true) {
 try {
 System.out.println("wait 开始");
 locker.wait();
 System.out.println("wait 结束");
 } catch (InterruptedException e) {
 e.printStackTrace();
 }
 }
 }
 }
}
static class NotifyTask implements Runnable {
 private Object locker;
 public NotifyTask(Object locker) {
 this.locker = locker;
 }
 @Override
 public void run() {
 synchronized (locker) {
 System.out.println("notify 开始");
 locker.notify();
 System.out.println("notify 结束");
 }
 }
}
public static void main(String[] args) throws InterruptedException {
 Object locker = new Object();
 Thread t1 = new Thread(new WaitTask(locker));
 Thread t2 = new Thread(new NotifyTask(locker));
 t1.start();
 Thread.sleep(1000);
 t2.start();
}
 
🚩notifyAll()方法
notify⽅法只是唤醒某⼀个等待线程. 使⽤notifyAll⽅法可以⼀次唤醒所有的等待线程.
 范例:使⽤notifyAll()⽅法唤醒所有等待线程, 在上⾯的代码基础上做出修改.
• 创建 3 个 WaitTask 实例. 1 个 NotifyTask 实例.
static class WaitTask implements Runnable {
 // 代码不变
}
static class NotifyTask implements Runnable {
 // 代码不变
}
public static void main(String[] args) throws InterruptedException {
 Object locker = new Object();
 Thread t1 = new Thread(new WaitTask(locker));
 Thread t3 = new Thread(new WaitTask(locker));
 Thread t4 = new Thread(new WaitTask(locker));
 Thread t2 = new Thread(new NotifyTask(locker));
 t1.start();
 t3.start();
 t4.start();
 Thread.sleep(1000);
 t2.start();
}
 
• 修改 NotifyTask 中的 run ⽅法, 把 notify 替换成 notifyAll
public void run() {
 synchronized (locker) {
 System.out.println("notify 开始");
 locker.notifyAll();
 System.out.println("notify 结束");
 }
}
 
此时可以看到, 调⽤ notifyAll 能同时唤醒 3 个wait 中的线程
注意: 虽然是同时唤醒 3 个线程, 但是这 3 个线程需要竞争锁. 所以并不是同时执⾏, ⽽仍然是有先有后的执⾏.
理解 notify 和 notifyAll
 notify 只唤醒等待队列中的⼀个线程. 其他线程还是乖乖等着
 
 notifyAll ⼀下全都唤醒, 需要这些线程重新竞争锁
 
⭕wait 和 sleep 的对比( 面试题)
其实理论上 wait 和 sleep 完全是没有可⽐性的,因为⼀个是⽤于线程之间的通信的,⼀个是让线程阻
 塞⼀段时间,
 唯⼀的相同点就是都可以让线程放弃执⾏⼀段时间.
当然为了⾯试的⽬的,我们还是总结下:
- wait 需要搭配 synchronized 使⽤. sleep 不需要.
 - wait 是 Object 的⽅法 sleep 是 Thread 的静态⽅法
 











![[C#]winform基于opencvsharp结合CSRNet算法实现低光图像增强黑暗图片变亮变清晰](https://img-blog.csdnimg.cn/direct/7e44321dfbb94c7e8bf5dcf7df2b7588.jpeg)






