1. CountDownLatch
CountDownLatch 是JUC提供的一个同步工具类,允许一个或者多个线程一直等待,直到其他线程的操作都执行完成之后再继续往下执行。CountDownLatch是通过“共享锁”实现的。在创建CountDownLatch中时,会传递一个int类型参数count,该参数是“锁计数器”的初始状态,表示该“共享锁”最多能被count个线程同时获取。当某线程调用该CountDownLatch对象的await()方法时,该线程会等待“共享锁”可用时,才能获取“共享锁”进而继续运行。而“共享锁”可用的条件,就是“锁计数器”的值为0!而“锁计数器”的初始值为count,每当一个线程调用该CountDownLatch对象的countDown()方法时,才将“锁计数器”-1;通过这种方式,必须有count个线程调用countDown()之后,“锁计数器”才为0,等待线程才能继续运行!
**使用场景:**在一些应用场合中,需要等待某个条件达到要求后才能做后面的事情;同时当线程都完成后也会触发事件,以便进行后面的操作。 这个时候就可以使用CountDownLatch。CountDownLatch最重要的方法是countDown()和await(),前者主要是计数减一,后者是等待计数到0,如果没有到达0,就继续阻塞等待。
package com.lchtest.juc.assistant;
import java.util.Random;
import java.util.concurrent.CountDownLatch;
import static java.lang.Thread.sleep;
public class CountDownLatchTest {
public static void main(String[] args) throws InterruptedException {
// 计数的初始值,要执行任务的线程的数量
CountDownLatch latch = new CountDownLatch(6);
for (int i = 1; i <= 6; i ++){
new Thread( () -> {
try {
sleep(new Random().nextInt(100));
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + "号同学离开了教室.");
latch.countDown(); // 每个子线程完成自己的工作后,计数减1
}, String.valueOf(i)).start();
}
// 在计数不为0的时候,主线程一直等待
latch.await();
System.out.println(Thread.currentThread().getName() + " 班长锁门了");
}
}
场景案例2: 三个线程共同拉取一块数据,每个线程拉取数据块的一部分,等到所有线程的数据都拉取过来之后,另一个处理线程再开始这个数据块。
package com.lchtest.juc.assistant;
import java.util.Random;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
// 下载线程
public class Downloader implements Runnable {
private CountDownLatch latch;
private String downloaderName;
public Downloader(CountDownLatch latch, String downloaderName){
this.downloaderName = downloaderName;
this.latch = latch;
}
@Override
public void run() {
this.download();
try {
TimeUnit.SECONDS.sleep(new Random().nextInt(10));
} catch (InterruptedException e) {
System.out.println("Thread interrupt status: " + Thread.currentThread().isInterrupted());
}
System.out.println(this.downloaderName + "下载完成...");
this.latch.countDown(); // 当前子任务执行完,计数减1
}
private void download() {
System.out.println(this.downloaderName + "正在下载文件...");
}
}
package com.lchtest.juc.assistant;
import java.util.concurrent.CountDownLatch;
public class DataProcessor implements Runnable {
private CountDownLatch latch;
public DataProcessor(CountDownLatch latch) {
this.latch = latch;
}
@Override
public void run() {
System.out.println("等待下载完数据...");
try {
this.latch.await();
} catch (InterruptedException e) {
System.out.println("Thread interrupt status: " + Thread.currentThread().isInterrupted());
}
System.out.println("数据下载完成, 开始处理数据...");
}
}
package com.lchtest.juc.assistant;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class CountDownLatchDataProcess {
public static void main(String[] args) {
ExecutorService executorService = Executors.newFixedThreadPool(4);
CountDownLatch latch = new CountDownLatch(3);
Downloader downloader1 = new Downloader(latch, "下载线程1");
Downloader downloader2 = new Downloader(latch, "下载线程2");
Downloader downloader3 = new Downloader(latch, "下载线程3");
DataProcessor dataProcessor = new DataProcessor(latch);
executorService.execute(downloader1);
executorService.execute(downloader2);
executorService.execute(downloader3);
executorService.execute(dataProcessor);
executorService.shutdown();
}
}
执行效果:
CountDownLatch原理图解参考https://www.itzhai.com/articles/graphical-several-fun-concurrent-helper-classes.html
CountDownLatch源码分析参考 https://www.cnblogs.com/skywang12345/p/3533887.html