1. 程式人生 > 其它 >JUC學習,CountDownLatch(執行緒計數器) 執行緒 和 Enum(列舉)

JUC學習,CountDownLatch(執行緒計數器) 執行緒 和 Enum(列舉)

技術標籤:JUCjava

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