1、前言:
来源周阳面试第二季
main
public class CountDownLatchDemo {
public static void main(String[] args) throws InterruptedException {
CountDownLatch countDownLatch=new CountDownLatch(6);
for (int i = 1; i <=6 ; i++) {
new Thread(()->{
System.out.println(Thread.currentThread().getName()+"国,被灭");
countDownLatch.countDown();
},CountryEnum.forEach_countryEnum(i).getRetMessage()).start();
}
countDownLatch.await();
System.out.println(Thread.currentThread().getName()+"\t **********************秦帝国,一统华夏");
}
}
枚举类
public enum CountryEnum{
ONE(1,"齐"),TWO(2,"楚"),THERE(3,"燕"),FOUR(4,"赵"),FIVE(5,"魏")
,SIX(6,"韩");
private final Integer retCode;
private final String retMessage;
CountryEnum(Integer retCode, String retMessage) {
this.retCode = retCode;
this.retMessage = retMessage;
}
public Integer getRetCode() {
return retCode;
}
public String getRetMessage() {
return retMessage;
}
public static CountryEnum forEach_countryEnum(int index){
CountryEnum[] values = CountryEnum.values();
for (CountryEnum value : values) {
if (index==value.retCode){
return value;
}
}
return null;
}
}
执行结果: