CSDN博主:【java_wxid】
CSDN博主:点击【Java廖志伟】
CSDN社区:点击【幕后大佬】
码云:点击【互联网Java工程师知识扫盲】
随笔 - 882,  文章 - 0,  评论 - 1,  阅读 - 51800

结合上一章阅读:https://blog.csdn.net/java_wxid/article/details/99168098

枚举代码:

package com.javaliao.backstage;

import lombok.Getter;

public enum MyData {

    ONE(1,"齐国"),TWO(2,"楚国"),THREE(3,"燕国"),FOUR(4,"赵国"),FIVE(5,"魏国"),SIX(6,"韩国");
    //提供get方法
    @Getter private Integer retCode;
    @Getter private String retMessage;
    //给上构造器
    MyData(Integer retCode, String retMessage) {
        this.retCode = retCode;
        this.retMessage = retMessage;
    }

    public static MyData forEatch_MyData(Integer retCode){
        MyData[] values = MyData.values();
        for (MyData value : values) {
            if(retCode == value.getRetCode()){
                return value;
            }
        }
        return null;
    }

}

使用枚举的代码:

import java.util.concurrent.CountDownLatch;


public class Demo {

    public static void main(String[] args) throws Exception{
        CountDownLatch countDownLatch = new CountDownLatch(5);
        for (int i = 1; i <= 6; i++) {
            new Thread(()->{
                System.out.println(Thread.currentThread().getName()+"\t 被灭");
                countDownLatch.countDown();
            },MyData.forEatch_MyData(i).getRetMessage()).start();
        }
        countDownLatch.await();
        System.out.println(Thread.currentThread().getName()+"\t 秦国一统华夏");
    }
}

控制台:

posted on   我是廖志伟  阅读(7)  评论(0编辑  收藏  举报  
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
历史上的今天:
2021-05-09 开发工具:第一章:软件安装包
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

南北踏尘
点击右上角即可分享
微信分享提示