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