问题场景
项目开发过程中当你有一个函数代码中需要处理多个任务,而多个任务之间执行顺序,数据并没有关联依赖,而当多个任务都执行完毕后,你又需要进行下一步处理数据,如下面代码所示:
package com.demo;
import lombok.SneakyThrows;
/**
* @author youyun.xu
* @Description: 串行执行任务
* @date 2022/01/15 21:05
*/
public class Demo {
@SneakyThrows
public static void main(String[] args) {
long startTime = System.currentTimeMillis();
//执行任务1 耗时5秒
Thread.sleep(5 * 1000l);
System.out.println("task 1 start -> cost 5 seconds");
//执行任务2 耗时8秒
Thread.sleep(8 * 1000l);
System.out.println("task 2 start -> cost 8 seconds");
long endTime = System.currentTimeMillis();
System.out.println("total cost ->" + (endTime - startTime) / 1000 + "秒");
//执行任务3
System.out.println("task 3 start");
}
}
问题分析
任务1执行需要5秒,任务2执行需要8秒,代码按顺序结构执行,编码的顺序A先执行,然后B再执行 ,整个过程是串行,如果按照这种思路写串行化的代码,执行总耗时达到13秒。如果不止2个,有更多的任务呢?
问题优化
JUC并发编程中,提供了丰富的并发编程类,你只需要根据自己的场景使用对应的工具类即可。比如像上面的问题可以考虑在主线程中启动多个异步线程并行执行,其中CountDownLatch提供了闭锁,可以帮助我们协调多个异步线程的执行状态。
举个例子
运动会跑步比赛,口令枪一响,各个线程(运动健儿)开始奔跑,你只需等他们全部跑到终点,然后你统计成绩计算排名公布结果。典型的多个线程等待一个线程、一个线程等待多个线程的场景,这里我们把场景转换成代码
package com.demo;
import lombok.SneakyThrows;
import java.util.concurrent.CountDownLatch;
/**
* @author youyun.xu
* @Description: JUC编程CountDownLatch
* @date 2022/1/15 21:05
*/
public class Demo1 {
@SneakyThrows
public static void main(String[] args) {
CountDownLatch countDownLatch = new CountDownLatch(2);
long startTime = System.currentTimeMillis();
new Thread(() -> {
try {
Thread.sleep(5 * 1000l);
System.out.println("task 1 start -> cost 5 seconds");
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
countDownLatch.countDown();
}
}).start();
new Thread(() -> {
try {
Thread.sleep(8 * 1000l);
System.out.println("task 2 start -> cost 8 seconds");
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
countDownLatch.countDown();
}
}).start();
long endTime = System.currentTimeMillis();
countDownLatch.await();
System.out.println("total cost ->" + (endTime - startTime) / 1000 + "秒");
}
}
我们发现主线程执行中,分别启动task 1、task 2 两个异步子线程执行任务,await() 会停留等待2个线程,等2个子线程都执行完毕了,再继续往下执行。从执行效率有明显提高。
CountDownLatch源码
1、CountDownLatch 构造函数
CountDownLatch 提供唯一一个有参数的构造函数给外部创建对象(new),参数count表示有几个需要任务数量。例如:
CountDownLatch countDownLatch = new CountDownLatch(2);
2、countDown 函数
countDown 使当前线程等待,直到闩锁计数到等于0,则继续往下执行。如果当前计数大于零,则它将递减。
countDownLatch.countDown();
3、await 函数
目前提供了2个重载的await 函数,一个无参,另外一个增加了闭锁时间。如果没有设置默认时间,则会一直等待。如果指定了时间,则只会在执行时间范围内等待,超过时间则自动打开闩锁,继续往下执行。详细规则如下:
* 如果当前计数值大于零,则当前线程会一直等待。
* 如果超过指定的等待时间,则当前线程不会再等待,则继续往下执行。
countDownLatch.await();
countDownLatch.await(10,TimeUnit.SECONDS);
3、闭锁如何实现
CountDownLatch 中的 Sync 继承了AbstractQueuedSynchronizer 对象,Sync 是 CountDownLatch 中的一个静态内部类,所有的操作的函数都来自父类。AbstractQueuedSynchronizer 底层实现为一个基于AQS实现的线程同步的队列,阻塞锁依赖 同步器(信号量、事件等)。它是一个先进先出(FIFO)等待队列,CLH锁通常用于自旋锁。用它来阻塞同步器。
Sync extends AbstractQueuedSynchronizer
思考题:
在你项目的开发任务中,有哪些场景是可以通过 CountDownLatch 来优化提升执行效率的?