标签:sys style 计算器 执行 ++ getter 列操作 inter enum
1.让一些线程阻塞直到另一些线程完成一系列操作后才被唤醒。
2.CountDownLatch主要有两个方法,当一个或多个线程调用await方法时,调用线程会被阻塞。其它线程调用countDown方法会将计算器减1(调用countDown方法的线程不会阻塞),当计数器的值变成零时,因调用await方法被阻塞的线程会被唤醒,继续执行。
例子:
1.先定义一个枚举类
public enum CountryEnum {
ONE(1,"齐"),TWO(2,"楚"),THREE(3,"燕"),FOUR(4,"赵"),FIVE(5,"魏"),SIX(6,"韩");
@Getter private Integer retCode;
@Getter private String retMessage;
CountryEnum(Integer retCode, String retMessage) {
this.retCode = retCode;
this.retMessage = retMessage;
}
public static CountryEnum forEach(int index) {
CountryEnum[] myArray = CountryEnum.values();
for(CountryEnum countryEnum : myArray) {
if(index == countryEnum.getRetCode()) {
return countryEnum;
}
}
return null;
}
}
2.
public class CountDownLatchDemo {
public static void main(String[] args) throws InterruptedException {
CountDownLatch countDownLatch = new CountDownLatch(6);
for(int i = 1; i<= 6; i++) {
new Thread(() ->{
countDownLatch.countDown();
System.out.println(Thread.currentThread().getName()+"\t国灭");
},CountryEnum.forEach(i).getRetMessage()).start();
}
//等待上面结果执行完
countDownLatch.await();
System.out.println(Thread.currentThread().getName()+"\t******秦国统一六国");
}
}
运行结果见下图:
标签:sys style 计算器 执行 ++ getter 列操作 inter enum
原文地址:https://www.cnblogs.com/liuyi13535496566/p/12150440.html