JUC学习,CountDownLatch(线程计数器) 线程 和 Enum(枚举)

01/16 10:39
阅读数 31

JUC学习,CountDownLatch(线程减法计数器) 线程 和 Enum(枚举)

package com.zhangye;

import java.util.concurrent.CountDownLatch;

public class CountDownLatchTest {

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

打印结果
齐 	 国被灭
燕 	 国被灭
楚 	 国被灭
赵 	 国被灭
韩 	 国被灭
魏 	 国被灭
main	 秦国统一华夏:

package com.zhangye;


public enum CountryEnum {
	
	ONE(1,"齐"),
	TWO(2,"燕"),
	THREE(3,"楚"),
	FOUR(4,"赵"),
	FIVE(5,"韩"),
	SIX(6,"魏");
	
	private Integer code;
	private String message;
	
	public Integer getCode() {
		return code;
	}
	public void setCode(Integer code) {
		this.code = code;
	}
	public String getMessage() {
		return message;
	}
	public void setMessage(String message) {
		this.message = message;
	}
	
	private CountryEnum(Integer code, String message) {
		this.code = code;
		this.message = message;
	}
	
	public static CountryEnum forEachEnum(int code){
		CountryEnum[] arr = CountryEnum.values();
		for (CountryEnum countryEnum : arr) {
			if(countryEnum.getCode() == code){
				return countryEnum;
			}
		}
		return null;
	}
	
	
	
}

展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部