public class Test04 { public static void main(String[] args) throws InterruptedException { CountDownLatch cd = new CountDownLatch(6);//总长度 for (int i = 1; i <=6 ; i++) { new Thread(()->{ System.out.println(Thread.currentThread().getName() +"\t 国被灭"); cd.countDown();//减一 },CountryEnum.forEach_CountryEnum(i).getMessage()).start(); } cd.await();//等待直到计数器为0 System.out.println(Thread.currentThread().getName() +"\t 秦灭六国,一统华夏"); } }
public enum CountryEnum { ONE(1,"齐"),TWO(2,"楚"),THREE(3,"燕"),FOUR(4,"赵"),FIVE(5,"魏"),SIX(6,"韩"); private int code; private String message; CountryEnum(int code, String message) { this.code = code; this.message = message; } public int getCode() { return code; } public void setCode(int code) { this.code = code; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } public static CountryEnum forEach_CountryEnum(int index){ CountryEnum[] values = CountryEnum.values(); for (CountryEnum countryEnum : values) { if(countryEnum.getCode()==index){ return countryEnum; } } return null; } }
原文:https://www.cnblogs.com/hpdblogs/p/12495299.html