欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-06-28 20:19:51
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);...

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

本文地址:https://blog.csdn.net/qq_29244151/article/details/112620451

相关标签: JUC java