首页 > 其他 > 详细

CountDownLatch(减少计数)

时间:2020-03-15 00:10:06      阅读:67      评论:0      收藏:0      [点我收藏+]

 

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


}

 

CountDownLatch(减少计数)

原文:https://www.cnblogs.com/hpdblogs/p/12495299.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!