1、通讯方式
要想实现多个线程之间的协同,如:线程执行的先后顺序、获取某条线程的执行结果等等。
涉及到线程之间的相互通讯,分为如下几类:
1) 文件共享
2) 网络共享
3) 共享变量
4) JDK提供的线程协调API
细分为:suspend()/resume()、wait()/notify()、park()/nupark()
2、文件共享
3、变量共享
4、线程协作—JDK API
JDK对需要多线程协作完成某一项任务的场景,提供了对应的API支持。
多线程协作的经典场景:生产者--消费者模型。(线程阻塞、线程唤醒)
示例:线程1去买包子,没有包子,则不再执行;线程2产出包子,产出后通知线程1执行。
1) API - 被弃用的 suspend()、resume()
作用:调用suspend()挂起目标线程,通过调用resume()可以恢复线程执行。
被弃用的主要原因是:容易写出死锁的代码。所以用wait/notify、park/unpark机制对它进行替代。
import java.util.concurrent.locks.LockSupport;
public class Demo6 {
/** 包子店 */
public static Object baozidian = null;
// suspend()、resume()对调用顺序有要求,也要开发自己注意锁的释放。这个被弃用的API, 容易死锁,也容易导致永久挂起。
/** 正常的suspend/resume */
public void suspendResumeTest() throws Exception {
// 启动线程
Thread consumerThread = new Thread(() -> {
if (baozidian == null) { // 如果没包子,则进入等待
System.out.println("1、进入等待");
Thread.currentThread().suspend();
}
System.out.println("2、买到包子,回家");
});
consumerThread.start();
// 3秒之后,生产一个包子
Thread.sleep(3000L);
baozidian = new Object();
consumerThread.resume();
System.out.println("3、通知消费者");
}
/** 死锁的suspend/resume。 suspend并不会像wait一样释放锁,故此容易写出死锁代码 */
public void suspendResumeDeadLockTest() throws Exception {
// 启动线程
Thread consumerThread = new Thread(() -> {
if (baozidian == null) { // 如果没包子,则进入等待
System.out.println("1、进入等待");
// 当前线程拿到锁,然后挂起
synchronized (this) {
Thread.currentThread().suspend();
}
}
System.out.println("2、买到包子,回家");
});
consumerThread.start();
// 3秒之后,生产一个包子
Thread.sleep(3000L);
baozidian = new Object();
// 争取到锁以后,再恢复consumerThread
synchronized (this) {
consumerThread.resume();
}
System.out.println("3、通知消费者");
}
/** 导致程序永久挂起的suspend/resume */
public void suspendResumeDeadLockTest2() throws Exception {
// 启动线程
Thread consumerThread = new Thread(() -> {
if (baozidian == null) {
System.out.println("1、没包子,进入等待");
try { // 为这个线程加上一点延时
Thread.sleep(5000L);
} catch (InterruptedException e) {
e.printStackTrace();
}
// 这里的挂起执行在resume后面
Thread.currentThread().suspend();
}
System.out.println("2、买到包子,回家");
});
consumerThread.start();
// 3秒之后,生产一个包子
Thread.sleep(3000L);
baozidian = new Object();
consumerThread.resume();
System.out.println("3、通知消费者");
consumerThread.join();
}
public static void main(String[] args) throws Exception {
// 对调用顺序有要求,也要开发自己注意锁的释放。这个被弃用的API, 容易死锁,也容易导致永久挂起。
// new Demo6().suspendResumeTest();
// new Demo6().suspendResumeDeadLockTest();
// new Demo6().suspendResumeDeadLockTest2();
}
}
2) API--wait()/notify()机制
这些方法只能由同一个对象锁的持有者线程调用;也就是写在同步代码块里面,否则会抛出
IllegalMonitorStateException异常。
wait()方法会导致当前线程进入等待状态,加入该对象的等待集合中,并且放弃当前持有的对象锁。
notify()/notifyAll()会唤醒一个或者所有正在等待当前对象锁的线程。
注意:虽然线程调用wait()方法时会释放对象锁,但是对顺序有要求,如果在调用notify()之后再调用wait()方法,
会导致线程永远处于waiting状态。
import java.util.concurrent.locks.LockSupport;
/** 三种线程协作通信的方式:suspend/resume、wait/notify、park/unpark */
public class Demo6 {
/** 包子店 */
public static Object baozidian = null;
// wait/notify要求再同步关键字里面使用,免去了死锁的困扰,但是一定要先调用wait,再调用notify,否则永久等待了
/** 正常的wait/notify */
public void waitNotifyTest() throws Exception {
// 启动线程
new Thread(() -> {
if (baozidian == null) { // 如果没包子,则进入等待
synchronized (this) {
try {
System.out.println("1、进入等待");
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
System.out.println("2、买到包子,回家");
}).start();
// 3秒之后,生产一个包子
Thread.sleep(3000L);
baozidian = new Object();
synchronized (this) {
this.notifyAll();
System.out.println("3、通知消费者");
}
}
/** 会导致程序永久等待的wait/notify */
public void waitNotifyDeadLockTest() throws Exception {
// 启动线程
new Thread(() -> {
if (baozidian == 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);
baozidian = new Object();
synchronized (this) {
this.notifyAll();
System.out.println("3、通知消费者");
}
}
public static void main(String[] args) throws Exception {
// wait/notify要求再同步关键字里面使用,免去了死锁的困扰,但是一定要先调用wait,再调用notify,否则永久等待了
// new Demo6().waitNotifyTest();
// new Demo6().waitNotifyDeadLockTest();
}
}
3)API-park()/unpark()机制
线程调用park()则等待 “许可” ,nupark()方法为指定线程提供 “许可(permit)”。
不要求park()与unpark()方法的调用顺序。
多次调用unpark()方法之后调用park(),线程会执行运行。但不会叠加,也就是说,连续多次调用park()方法,
第一次会拿到 “许可” 直接运行,后续调用park()会进入等待状态。
注意:park()根本没有锁的概念,所以不会释放锁,故在同步代码块中使用时需要注意。
import java.util.concurrent.locks.LockSupport;
/** 三种线程协作通信的方式:suspend/resume、wait/notify、park/unpark */
public class Demo6 {
/** 包子店 */
public static Object baozidian = null;
// park/unpark没有顺序要求,但是park并不会释放锁,因为park没有锁的概念,所有再同步代码中使用要注意
/** 正常的park/unpark */
public void parkUnparkTest() throws Exception {
// 启动线程
Thread consumerThread = new Thread(() -> {
if (baozidian == null) { // 如果没包子,则进入等待
System.out.println("1、进入等待");
LockSupport.park();
}
System.out.println("2、买到包子,回家");
});
consumerThread.start();
// 3秒之后,生产一个包子
Thread.sleep(3000L);
baozidian = new Object();
LockSupport.unpark(consumerThread);
System.out.println("3、通知消费者");
}
/** 死锁的park/unpark */
public void parkUnparkDeadLockTest() throws Exception {
// 启动线程
Thread consumerThread = new Thread(() -> {
if (baozidian == null) { // 如果没包子,则进入等待
System.out.println("1、进入等待");
// 当前线程拿到锁,然后挂起
synchronized (this) {
LockSupport.park();
}
}
System.out.println("2、买到包子,回家");
});
consumerThread.start();
// 3秒之后,生产一个包子
Thread.sleep(3000L);
baozidian = new Object();
// 争取到锁以后,再恢复consumerThread
synchronized (this) {
LockSupport.unpark(consumerThread);
}
System.out.println("3、通知消费者");
}
public static void main(String[] args) throws Exception {
// park/unpark没有顺序要求,但是park并不会释放锁,所有再同步代码中使用要注意
// new Demo6().parkUnparkTest();
// new Demo6().parkUnparkDeadLockTest();
}
}
5、伪唤醒
警告!上面代码使用了if语句判断线程是否进入等待状态,是错误的!
官方建议,应该在循环中检查等待条件;原因是处于等待状态的线程可能会收到错误警报和伪唤醒。
如果不在循环中检查等待条件,程序就会在没有满足条件的情况下退出。
伪唤醒是指线程并非因为notify、notifyAll、unpark等API调用而唤醒,是更底层的原因导致的。
//wait()
synchronized(obj){
while(<条件判断>){
obj.wait()
...
}
}
//park()
while(<条件判断>){
LockSupport.park()
...
}
7、以上整体代码
import java.util.concurrent.locks.LockSupport;
/** 三种线程协作通信的方式:suspend/resume、wait/notify、park/unpark */
public class Demo6 {
/** 包子店 */
public static Object baozidian = null;
// suspend()、resume()对调用顺序有要求,也要开发自己注意锁的释放。这个被弃用的API, 容易死锁,也容易导致永久挂起。
/** 正常的suspend/resume */
public void suspendResumeTest() throws Exception {
// 启动线程
Thread consumerThread = new Thread(() -> {
if (baozidian == null) { // 如果没包子,则进入等待
System.out.println("1、进入等待");
Thread.currentThread().suspend();
}
System.out.println("2、买到包子,回家");
});
consumerThread.start();
// 3秒之后,生产一个包子
Thread.sleep(3000L);
baozidian = new Object();
consumerThread.resume();
System.out.println("3、通知消费者");
}
/** 死锁的suspend/resume。 suspend并不会像wait一样释放锁,故此容易写出死锁代码 */
public void suspendResumeDeadLockTest() throws Exception {
// 启动线程
Thread consumerThread = new Thread(() -> {
if (baozidian == null) { // 如果没包子,则进入等待
System.out.println("1、进入等待");
// 当前线程拿到锁,然后挂起
synchronized (this) {
Thread.currentThread().suspend();
}
}
System.out.println("2、买到包子,回家");
});
consumerThread.start();
// 3秒之后,生产一个包子
Thread.sleep(3000L);
baozidian = new Object();
// 争取到锁以后,再恢复consumerThread
synchronized (this) {
consumerThread.resume();
}
System.out.println("3、通知消费者");
}
/** 导致程序永久挂起的suspend/resume */
public void suspendResumeDeadLockTest2() throws Exception {
// 启动线程
Thread consumerThread = new Thread(() -> {
if (baozidian == null) {
System.out.println("1、没包子,进入等待");
try { // 为这个线程加上一点延时
Thread.sleep(5000L);
} catch (InterruptedException e) {
e.printStackTrace();
}
// 这里的挂起执行在resume后面
Thread.currentThread().suspend();
}
System.out.println("2、买到包子,回家");
});
consumerThread.start();
// 3秒之后,生产一个包子
Thread.sleep(3000L);
baozidian = new Object();
consumerThread.resume();
System.out.println("3、通知消费者");
consumerThread.join();
}
// wait/notify要求再同步关键字里面使用,免去了死锁的困扰,但是一定要先调用wait,再调用notify,否则永久等待了
/** 正常的wait/notify */
public void waitNotifyTest() throws Exception {
// 启动线程
new Thread(() -> {
if (baozidian == null) { // 如果没包子,则进入等待
synchronized (this) {
try {
System.out.println("1、进入等待");
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
System.out.println("2、买到包子,回家");
}).start();
// 3秒之后,生产一个包子
Thread.sleep(3000L);
baozidian = new Object();
synchronized (this) {
this.notifyAll();
System.out.println("3、通知消费者");
}
}
/** 会导致程序永久等待的wait/notify */
public void waitNotifyDeadLockTest() throws Exception {
// 启动线程
new Thread(() -> {
if (baozidian == 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);
baozidian = new Object();
synchronized (this) {
this.notifyAll();
System.out.println("3、通知消费者");
}
}
// park/unpark没有顺序要求,但是park并不会释放锁,所有再同步代码中使用要注意
/** 正常的park/unpark */
public void parkUnparkTest() throws Exception {
// 启动线程
Thread consumerThread = new Thread(() -> {
if (baozidian == null) { // 如果没包子,则进入等待
System.out.println("1、进入等待");
LockSupport.park();
}
System.out.println("2、买到包子,回家");
});
consumerThread.start();
// 3秒之后,生产一个包子
Thread.sleep(3000L);
baozidian = new Object();
LockSupport.unpark(consumerThread);
System.out.println("3、通知消费者");
}
/** 死锁的park/unpark */
public void parkUnparkDeadLockTest() throws Exception {
// 启动线程
Thread consumerThread = new Thread(() -> {
if (baozidian == null) { // 如果没包子,则进入等待
System.out.println("1、进入等待");
// 当前线程拿到锁,然后挂起
synchronized (this) {
LockSupport.park();
}
}
System.out.println("2、买到包子,回家");
});
consumerThread.start();
// 3秒之后,生产一个包子
Thread.sleep(3000L);
baozidian = 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();
}
}