枚举加countdownLatch的使用

Posted xiufengchen

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了枚举加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"+"	"+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的使用的主要内容,如果未能解决你的问题,请参考以下文章

Java并发多线程编程——CountDownLatch

我该如何做模态对话框片段(代码在我关闭之前不会执行)

面试官:CountDownLatch 与 CyclicBarrier 的使用场景?有什么区别?

Redis闭锁CountDownLatch

CountDownLatch用法

带有红宝石集合/可枚举的酷技巧和富有表现力的片段[关闭]