CountDownLatch(减少计数)

 

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;
    }


}

 

posted @ 2020-03-14 23:53  常温的冰  阅读(372)  评论(0)    收藏  举报