上节说了CPU缓存和内存屏障,CPU厂家考虑到指令重排的一些解决方案吧,本次说说线程通信,多个线程运行期间,它们之间进行数据交互和协作。
![](https://img.haomeiwen.com/i11223715/4120779154a2a6da.png)
(一)通信的方式
- ①介绍
多要实现多线程之间的协同,如:线程执行先后顺序,获取某一个线程的执行的结果等等。这个过程就涉及到线程之间的项目通信。
1.文件共享
2.网络共享
3.变量共享
4.jdk提供的线程协调API (重点)
wait/notify,park/unpark
- ② 文件共享
线程1 写入文件a.txt 数据,线程2读取a.txt数据。
![](https://img.haomeiwen.com/i11223715/ccfb37d6ae0b26eb.png)
- ③ 网络共享
ServerThread ,socket的方式,目前java很少做cs开发了,基本不做概述了。
- ④ 变量共享
定义了一个静态变量,content,一个线程往变量里面写数据,一个线程往变量里面读数据。
![](https://img.haomeiwen.com/i11223715/bf7d3fe1c053feaf.png)
(二)线程协作
- ① 介绍
JDK 中对于需要多线程协作完成某一任务的场景,提供了对应API支持,多线程协作的经典场景就是 生产者 --消费者模式。用到了线程阻塞,线程唤醒。
- ② 场景
线程1去吃馒头,没有馒头,就不执行。线程2生产馒头,有了就执行线程1可以过来吃了。 线程1是消费者,线程2是生产者,这种模型广泛应用在分布式系统架构开发上,以及底层组件的开发上。 需要考虑的是如何做到通知,线程2如何通知线程1继续去跑,
![](https://img.haomeiwen.com/i11223715/f1ae37df3d3503b6.png)
- ③ API-被弃用的suspend 和 resume
调用suspend 挂起目标线程,通过resume可以恢复线程执行。
public void suspendResumeTest() throws Exception {
// 启动线程
Thread consumerThread = new Thread(new Runnable() {
public void run() {
if (mantoudian == null) { // 如果没馒头,则进入等待
System.out.println("1、进入等待");
Thread.currentThread().suspend();
}
System.out.println("3、买到馒头,回家");
}
});
consumerThread.start();
// 3秒之后,生产一个馒头
Thread.sleep(3000L);
mantoudian = new Object();
System.out.println("2、通知消费者");
consumerThread.resume();
}
方法看起来很简单,通过名称控制线程的挂起和恢复。
被启用的主要原因:容易写出死锁的代码。
![](https://img.haomeiwen.com/i11223715/2a11ab6e09b4fff2.png)
演示死锁,对使用的要求非常的严格,不容易控制。
- 同步代码中使用
public void suspendResumeDeadLockTest() throws Exception {
// 启动线程
Thread consumerThread = new Thread(new Runnable() {
public void run() {
if (mantoudian == null) { // 如果没馒头,则进入等待
System.out.println("1、进入等待");
// 当前线程拿到锁,然后挂起
synchronized (this) {
Thread.currentThread().suspend();
}
}
System.out.println("3、买到馒头,回家");
}
});
consumerThread.start();
// 3秒之后,生产一个馒头
Thread.sleep(3000L);
mantoudian = new Object();
// 争取到锁以后,再恢复consumerThread
synchronized (this) {
consumerThread.resume();
}
System.out.println("2、通知消费者");
}
这个代码的意思当进入【馒头店】没有馒头的时候,消费者把喇叭给拿走了,拿回家等待喇叭通知他有馒头的时候通知他,结果导致生产者去找喇叭的时候找不到,无法通知消费者。就进入了死锁的情况。同步块就是锁了。
![](https://img.haomeiwen.com/i11223715/b6322c34ed355deb.png)
- suspend 比resume后执行
/** 导致程序永久挂起的suspend/resume */
public void suspendResumeDeadLockTest2() throws Exception {
// 启动线程
Thread consumerThread = new Thread(new Runnable() {
public void run() {
if (mantoudian == null) {
System.out.println("1、没馒头,进入等待");
try { // 为这个线程加上一点延时
Thread.sleep(5000L);
} catch (InterruptedException e) {
e.printStackTrace();
}
// 这里的挂起执行在resume后面
Thread.currentThread().suspend();
}
System.out.println("3、买到馒头,回家");
}
});
consumerThread.start();
// 3秒之后,生产一个馒头
Thread.sleep(3000L);
mantoudian = new Object();
consumerThread.resume();
System.out.println("2、通知消费者");
consumerThread.join();
}
这个代码的意思:12点的时候消费者进店了,他说他去买个菜就回来,12点10分的时候馒头店生产者喊馒头来了,但是因为消费者这10分钟不在现场没听到喇叭喊,12点11分他过来之后就一直等喇叭喊,其实喇叭早就喊过了,他不知道,他就一直在门口等。
![](https://img.haomeiwen.com/i11223715/e8a5a0cd437a7cd7.png)
- ③ API-wait / notify 机制
方法只能由统一对象锁的持有者线程调用,也就是写在同步块里面,否则会抛出illegalMonitorStateException异常。
wait方法导致当前线程等待,加入该对象的等待集合中,并且放弃当前持有的对象锁。notify 、 notifyAll方法唤醒一个或所有正在等待这个对象锁的线程。
注意:虽然会wait自动解锁,但是对顺序是有要求的,如果在notify被调用之后,才开始wait方法的调用,线程会永远处于WAITING状态。
正常的情况
public void waitNotifyTest() throws Exception {
// 启动线程
new Thread(new Runnable() {
public void run() {
if (mantoudian == null) { // 如果没馒头,则进入等待
synchronized (this) {
try {
System.out.println("1、进入等待");
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
System.out.println("2、买到馒头,回家");
}
}).start();
// 3秒之后,生产一个馒头
Thread.sleep(3000L);
mantoudian = new Object();
synchronized (this) {
this.notifyAll();
System.out.println("3、通知消费者");
}
}
注意:先后顺序,避免死锁代码、
/** 会导致程序永久等待的wait/notify */
public void waitNotifyDeadLockTest() throws Exception {
// 启动线程
new Thread(new Runnable() {
public void run() {
if (mantoudian == null) { // 如果没馒头,则进入等待
try {
Thread.sleep(5000L);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
synchronized (this) {
try {
System.out.println("1、进入等待");
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
System.out.println("2、买到馒头,回家");
}
}).start();
// 3秒之后,生产一个馒头
Thread.sleep(3000L);
mantoudian = new Object();
synchronized (this) {
this.notifyAll();
System.out.println("3、通知消费者");
}
}
通知消费者,进入等待状态,不动了,来晚了。注意时间问题。
- ⑤ park 和 unpark
park是等待一个许可,unpark是为某线程提供一个许可。
先提供许可。当某线程调用park时,已经有许可了,它就消费这个许可,然后可以继续运行。这其实是必须的。考虑最简单的生产者消费者(模型:消费者需要消费一个资源,于是调用park操作等待;生产者则生产资源,然后调用unpark给予消费者使用的许可。非常有可能的一种情况是,生产者先生产,这时候消费者可能还没有构造好(比如线程还没启动,或者还没切换到该线程)。那么等消费者准备好要消费时,显然这时候资源已经生产好了,可以直接用,那么park操作当然可以直接运行下去。如果没有这个语义,那将非常难以操作。
park 和 unpark没有调用顺序要求。wait和notifyAll有顺序性。本身不是基于监视器锁的概念去实现的。
public void parkUnparkTest() throws Exception {
// 启动线程
Thread consumerThread = new Thread(new Runnable() {
public void run() {
if (mantoudian == null) { // 如果没馒头,则进入等待
System.out.println("1、进入等待");
LockSupport.park();
}
System.out.println("2、买到馒头,回家");
}
});
consumerThread.start();
// 3秒之后,生产一个馒头
Thread.sleep(3000L);
mantoudian = new Object();
LockSupport.unpark(consumerThread);
System.out.println("3、通知消费者");
}
![](https://img.haomeiwen.com/i11223715/e083b9c0bfc82d52.png)
public void parkUnparkDeadLockTest() throws Exception {
// 启动线程
Thread consumerThread = new Thread(new Runnable() {
public void run() {
if(mantoudian ==null)
{ // 如果没馒头,则进入等待
System.out.println("1、进入等待");
// 当前线程拿到锁,然后挂起
synchronized (this) {
LockSupport.park();
}
}
System.out.println("2、买到馒头,回家");
}
});
consumerThread.start();
// 3秒之后,生产一个馒头
Thread.sleep(3000L);
mantoudian = new Object();
// 争取到锁以后,再恢复consumerThread
synchronized (this) {
LockSupport.unpark(consumerThread);
}
System.out.println("3、通知消费者");
}
用了同步块,这把锁没人可以拿得到。资源被死锁了。全部都gg了。
(三)伪唤醒
上面的代码用if语句来判断,是否进入等待状态,是错误的!
官方建议应该在循环中检查等待条件,原因处于等待状态的线程可能收到错误警告和伪唤醒,如果不在循环中检查等待条件,程序就会在没有满足结束条件的情况下退出。
伪唤醒是指线程并非因为notify,notifyall,uppark等api调用而唤醒,是更底层原因导致的。
synchronized (obj) {
while (<condition does not hold>)
obj.wait();
... // Perform action appropriate to condition
}
//该方法只能由作为该对象的监视器的所有者的线程调用。 有关线程可以成为监视器所有者的方式的说明,请参阅notify方法。
完整代码
import java.util.concurrent.locks.LockSupport;
/** 三种线程协作通信的方式:suspend/resume、wait/notify、park/unpark */
public class Demo6 {
/** 馒头店 */
public static Object mantoudian = null;
/** 正常的suspend/resume */
public void suspendResumeTest() throws Exception {
// 启动线程
Thread consumerThread = new Thread(new Runnable() {
public void run() {
if (mantoudian == null) { // 如果没馒头,则进入等待
System.out.println("1、进入等待");
Thread.currentThread().suspend();
}
System.out.println("3、买到馒头,回家");
}
});
consumerThread.start();
// 3秒之后,生产一个馒头
Thread.sleep(3000L);
mantoudian = new Object();
System.out.println("2、通知消费者");
consumerThread.resume();
}
/** 死锁的suspend/resume。 suspend并不会像wait一样释放锁,故此容易写出死锁代码 */
public void suspendResumeDeadLockTest() throws Exception {
// 启动线程
Thread consumerThread = new Thread(new Runnable() {
public void run() {
if (mantoudian == null) { // 如果没馒头,则进入等待
System.out.println("1、进入等待");
// 当前线程拿到锁,然后挂起
synchronized (this) {
Thread.currentThread().suspend();
}
}
System.out.println("3、买到馒头,回家");
}
});
consumerThread.start();
// 3秒之后,生产一个馒头
Thread.sleep(3000L);
mantoudian = new Object();
// 争取到锁以后,再恢复consumerThread
synchronized (this) {
consumerThread.resume();
}
System.out.println("2、通知消费者");
}
/** 导致程序永久挂起的suspend/resume */
public void suspendResumeDeadLockTest2() throws Exception {
// 启动线程
Thread consumerThread = new Thread(new Runnable() {
public void run() {
if (mantoudian == null) {
System.out.println("1、没馒头,进入等待");
try { // 为这个线程加上一点延时
Thread.sleep(5000L);
} catch (InterruptedException e) {
e.printStackTrace();
}
// 这里的挂起执行在resume后面
Thread.currentThread().suspend();
}
System.out.println("3、买到馒头,回家");
}
});
consumerThread.start();
// 3秒之后,生产一个馒头
Thread.sleep(3000L);
mantoudian = new Object();
consumerThread.resume();
System.out.println("2、通知消费者");
consumerThread.join();
}
/** 正常的wait/notify */
public void waitNotifyTest() throws Exception {
// 启动线程
new Thread(new Runnable() {
public void run() {
if (mantoudian == null) { // 如果没馒头,则进入等待
synchronized (this) {
try {
System.out.println("1、进入等待");
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
System.out.println("2、买到馒头,回家");
}
}).start();
// 3秒之后,生产一个馒头
Thread.sleep(3000L);
mantoudian = new Object();
synchronized (this) {
this.notifyAll();
System.out.println("3、通知消费者");
}
}
/** 会导致程序永久等待的wait/notify */
public void waitNotifyDeadLockTest() throws Exception {
// 启动线程
new Thread(new Runnable() {
public void run() {
if (mantoudian == null) { // 如果没馒头,则进入等待
try {
Thread.sleep(5000L);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
synchronized (this) {
try {
System.out.println("1、进入等待");
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
System.out.println("2、买到馒头,回家");
}
}).start();
// 3秒之后,生产一个馒头
Thread.sleep(3000L);
mantoudian = new Object();
synchronized (this) {
this.notifyAll();
System.out.println("3、通知消费者");
}
}
/** 正常的park/unpark */
public void parkUnparkTest() throws Exception {
// 启动线程
Thread consumerThread = new Thread(new Runnable() {
public void run() {
if (mantoudian == null) { // 如果没馒头,则进入等待
System.out.println("1、进入等待");
LockSupport.park();
}
System.out.println("2、买到馒头,回家");
}
});
consumerThread.start();
// 3秒之后,生产一个馒头
Thread.sleep(3000L);
mantoudian = new Object();
LockSupport.unpark(consumerThread);
System.out.println("3、通知消费者");
}
/** 死锁的park/unpark */
public void parkUnparkDeadLockTest() throws Exception {
// 启动线程
Thread consumerThread = new Thread(new Runnable() {
public void run() {
while(mantoudian ==null)
{ // 如果没馒头,则进入等待
System.out.println("1、进入等待");
// 当前线程拿到锁,然后挂起
synchronized (this) {
LockSupport.park();
}
}
System.out.println("2、买到馒头,回家");
}
});
consumerThread.start();
// 3秒之后,生产一个馒头
Thread.sleep(3000L);
mantoudian = new Object();
// 争取到锁以后,再恢复consumerThread
synchronized (this) {
LockSupport.unpark(consumerThread);
}
System.out.println("3、通知消费者");
}
public static void main(String[] args) throws Exception {
// 对调用顺序有要求,也要开发自己注意锁的释放。这个被弃用的API, 容易死锁,也容易导致永久挂起。
//new Demo6().suspendResumeTest();
// new Demo6().suspendResumeDeadLockTest();
// new Demo6().suspendResumeDeadLockTest2();
// wait/notify要求再同步关键字里面使用,免去了死锁的困扰,但是一定要先调用wait,再调用notify,否则永久等待了
// new Demo6().waitNotifyTest();
// new Demo6().waitNotifyDeadLockTest();
// park/unpark没有顺序要求,但是park并不会释放锁,所有在同步代码中使用要注意
// new Demo6().parkUnparkTest();
// new Demo6().parkUnparkDeadLockTest();
}
}
PS:suspend 和 resume对调用顺序有要求,注意锁的释放。这个被弃用的API, 容易死锁,也容易导致永久挂起。wait/notify要求再同步关键字里面使用,免去了死锁的困扰,但是一定要先调用wait,再调用notify,否则永久等待了。park/unpark没有顺序要求,但是park并不会释放锁,所有在同步代码中使用要注意。上边的代码使用了if判断,应该将if更改为while这样才不会出现伪唤醒产生的逻辑问题。之后一起开发JDK多线程工具类的底层实现原理。这次很重要。
网友评论