枚举加countdownLatch的使用

package com.cxy.juc;

import java.util.concurrent.CountDownLatch;

public class CountDownlatchDemo {
    public static void main(String[] args) {
        CountDownLatch countDownLatch =new CountDownLatch(6);
        for (int i = 1; i <=6 ; i++) {
            new Thread(()->{
                System.out.println("1"+"\t"+Thread.currentThread().getName());
                countDownLatch.countDown();
            },Count.fore(i).getMsg()).start();
        }
        try {
            countDownLatch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("2");


    }
}
package com.cxy.juc;

import com.sun.javafx.scene.control.behavior.TwoLevelFocusBehavior;

public enum Count {

    ONE (1,"A"),TWO(2,"B"),THREE(3,"C"),FOUR(4,"D"),FIVE(5,"E"),SIX(6,"F");
    private  Integer num;
    private  String msg;

    Count(Integer num, String msg) {
        this.num = num;
        this.msg = msg;
    }

    public Integer getNum() {
        return num;
    }

    public void setNum(Integer num) {
        this.num = num;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public  static  Count fore(int num){
        Count[] myArray =Count.values();
        for (Count count:myArray){
            if (num ==count.getNum()){
                return count;
            }
        }
        return null;
    }
}

 

上一篇:CountDownLatch 实现公平测量cas、synchronized以及ReenTrantLock并发锁性能


下一篇:CountDownLatch和CyclicBarrier使用上的区别