JUC-线程协作-0-CountDownLatch

线程的运行和运行时机是不受我们自己控制的,一些情况下我们可能需要多个线程之间配合来完成一些任务,比如线程A等B线程执行完成之后运行等

CountDownLatch

CountDownLatch 是一个线程协作的工具类,是一个计数器,主要用到的有以下几个方法:

  • 构造方法,参数是 count ,是需要倒数的值
  • await(): 调用 await() 方法的线程会被挂起,等到 count 为 0 的时候才会继续执行
  • countDown(): 每次调用会将 count 值减1,直到为0的时候等待的线程就会被唤醒

常见用法

用法一

CountDownLatch 的第一种用法就是一个线程等待多个线程都执行完毕,再继续自己的工作,代码示例如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
@Slf4j
public class CountDownLatchTest {

public static void main(String[] args) throws InterruptedException {
ExecutorService threadPool = Executors.newFixedThreadPool(5);
CountDownLatch countDownLatch = new CountDownLatch(5);

for (int i = 0; i < 5; i++) {
threadPool.submit(()->{
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}finally {
log.info("已完成...");
countDownLatch.countDown();
}
});
}

log.info("正在等待其他线程....");
countDownLatch.await();
log.info("所有线程执行完毕...");

}

}

运行输出如下:

1
2
3
4
5
6
7
10:25:42.359 [main] INFO com.learning.java.cooperation.CountDownLatchTest - 正在等待其他线程....
10:25:43.352 [pool-1-thread-2] INFO com.learning.java.cooperation.CountDownLatchTest - 已完成...
10:25:43.352 [pool-1-thread-3] INFO com.learning.java.cooperation.CountDownLatchTest - 已完成...
10:25:43.352 [pool-1-thread-1] INFO com.learning.java.cooperation.CountDownLatchTest - 已完成...
10:25:43.353 [pool-1-thread-5] INFO com.learning.java.cooperation.CountDownLatchTest - 已完成...
10:25:43.353 [pool-1-thread-4] INFO com.learning.java.cooperation.CountDownLatchTest - 已完成...
10:25:43.353 [main] INFO com.learning.java.cooperation.CountDownLatchTest - 所有线程执行完毕...

首先新建一个 count 为 5 的 CountDownLatch 计数器,然后新建5个线程,之后主线程调用 await() 等待,等待5个线程都调用了 countDown() 之后, count 为 0, 主线程继续运行

用法二

第二种用法是多个线程等待某一个线程的信号,然后同时开始执行,代码示例如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
@Slf4j
public class CountDownLatchTest {

public static void main(String[] args) throws InterruptedException {
ExecutorService threadPool = Executors.newFixedThreadPool(5);
CountDownLatch countDownLatch = new CountDownLatch(1);

for (int i = 0; i < 5; i++) {
threadPool.submit(()->{
try {
log.info("就绪,等待主线程信号");
// 挂起线程,等待主线程信号
countDownLatch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}finally {
log.info("已完成...");
}
});
}

Thread.sleep(2000);
log.info("主线程准备完毕..");
countDownLatch.countDown();
}
}

控制台输出如下

1
2
3
4
5
6
7
8
9
10
11
10:41:52.079 [pool-1-thread-1] INFO com.learning.java.cooperation.CountDownLatchTest - 就绪,等待主线程信号
10:41:52.079 [pool-1-thread-5] INFO com.learning.java.cooperation.CountDownLatchTest - 就绪,等待主线程信号
10:41:52.080 [pool-1-thread-2] INFO com.learning.java.cooperation.CountDownLatchTest - 就绪,等待主线程信号
10:41:52.088 [pool-1-thread-3] INFO com.learning.java.cooperation.CountDownLatchTest - 就绪,等待主线程信号
10:41:52.092 [pool-1-thread-4] INFO com.learning.java.cooperation.CountDownLatchTest - 就绪,等待主线程信号
10:41:54.072 [main] INFO com.learning.java.cooperation.CountDownLatchTest - 主线程准备完毕..
10:41:54.072 [pool-1-thread-1] INFO com.learning.java.cooperation.CountDownLatchTest - 已完成...
10:41:54.072 [pool-1-thread-5] INFO com.learning.java.cooperation.CountDownLatchTest - 已完成...
10:41:54.072 [pool-1-thread-2] INFO com.learning.java.cooperation.CountDownLatchTest - 已完成...
10:41:54.072 [pool-1-thread-3] INFO com.learning.java.cooperation.CountDownLatchTest - 已完成...
10:41:54.072 [pool-1-thread-4] INFO com.learning.java.cooperation.CountDownLatchTest - 已完成...

这种场景一般用于压测, 多个线程准备好之后,一起发送请求给服务器

总结

  • 了解 CountDownLatch 两种常见的使用场景: 一等多和多等一
  • CountDownLatch 实例化的时候需要传入倒数的次数,等到倒数为0的时候,所有调用过 await() 方法的线程就会被唤醒
  • CountDownLatch 不能回滚重置,也就是不能重复使用