让一些线程阻塞直到另一些线程完成一系列操作后才被唤醒。
CountDownLatch主要有两个方法,当一个或多个线程调用await方法时,这些线程会阻塞。其它线程调用countDown方法会将计数器减1(调用countDown方法的线程不会阻塞), 当计数器的值变为0时,因await方法阻塞的线程会被唤醒,继续执行。 解释:5个同学陆续离开教室后值班同学才可以关门。也即 秦灭6国,一统华夏main主线程必须要等前面5个线程完成全部工作后,自己才能开干
public class CountDownLatchDemo
{
public static void main(String[] args) throws InterruptedException
{
//秦灭6国,一统华夏
CountDownLatch cdl = new CountDownLatch(6);
for (int i = 1; i <=6; i++)
{
new Thread(() -> {
System.out.println(Thread.currentThread().getName()+"\t 国被灭");
cdl.countDown();
},CountryEnums.forEachCountryEnums(i).getRetMsg()).start();
}
cdl.await();
System.out.println(Thread.currentThread().getName()+"*********秦灭6国,一统华夏");
}
}
public enum CountryEnums {
ONE(1,"韩"),TWO(2,"魏"),THREE(3,"赵"),FOUR(4,"燕"),FIVE(5,"齐"),SIX(6,"楚");
private Integer retCode;
private String retMsg;
private CountryEnums(Integer retCode, String retMsg) {
this.retCode = retCode;
this.retMsg = retMsg;
}
public Integer getRetCode() {
return retCode;
}
public void setRetCode(Integer retCode) {
this.retCode = retCode;
}
public String getRetMsg() {
return retMsg;
}
public void setRetMsg(String retMsg) {
this.retMsg = retMsg;
}
public static CountryEnums forEachCountryEnums(Integer index) {
for (CountryEnums enums : values()) {
if(enums.getRetCode() == index) {
return enums;
}
}
return null;
}
}