CountDownLatch:用于多线程并行执行
例:
@Slf4j
public class CountDownLatchDemo {
private static volatile int index = 0;
public static void main(String[] args) throws InterruptedException {
//创建倒计数器,指定计数数量为3
CountDownLatch countDownLatch = new CountDownLatch(3);
ThreadPoolManager.ThreadPool threadPool = ThreadPoolManager.getThreadPool();
for (int i = 1; i < 4; i++) {
int s=++index;
threadPool.execute(() -> {
log.info(s + " runing " + System.currentTimeMillis());
try {
Thread.sleep(1000);
log.info(s + " run end " + System.currentTimeMillis());
} catch (InterruptedException e) {
log.error(e.getMessage(), e);
} finally {
//通知CountDownLatch有一个线程已经准备完毕,倒计数器可以减一了